4
我正在開發增強現實遊戲,並且在設備的方向鎖定打開時遇到了相機視圖方向的問題。強制攝像機視圖在方向鎖定的橫向上
我使用這個代碼加載一個視圖內的相機視圖:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.sessionView.bounds;
[self.sessionView.layer addSublayer:captureVideoPreviewLayer];
CGRect bounds=sessionView.layer.bounds;
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.bounds=bounds;
captureVideoPreviewLayer.orientation = [[UIDevice currentDevice] orientation];
captureVideoPreviewLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// device.position ;
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if ([device hasTorch]) {
([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720]);
}
else {
([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480]);
}
[session addInput:input];
[session startRunning];
並保持應用程序的方向橫向的權利,我有選擇的Xcode的應用程序摘要只有那個盒子,搭配:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
當設備對方向鎖定(雙擊home鍵,向右滑動,點擊方向圖標)相機的看法是在肖像,與遊戲中的景觀休息。有沒有什麼辦法解決這一問題?從我讀過的內容來看,用戶打開遊戲時無法關閉方向鎖定。
一直在尋找一個解決方案,爲年齡。謝謝! –
+1爲captureVideoPreviewLayer.connection.videoOrientation。謝謝。 – iLearner