2012-12-12 122 views
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鍵,向右滑動,點擊方向圖標)相機的看法是在肖像,與遊戲中的景觀休息。有沒有什麼辦法解決這一問題?從我讀過的內容來看,用戶打開遊戲時無法關閉方向鎖定。

回答

15

您的預覽圖層無法定位的原因是由於您使用的是棄用的API,而且在更改設備方向時未更新視頻方向。

  1. 在你的代碼,即

    captureVideoPreviewLayer.connection.videoOrientation 
    
  2. 更新在shouldAutorotate視頻取向刪除

    captureVideoPreviewLayer.orientation 
    

    使用videoOrientation財產的過時的API即改爲如下:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
    
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
        { 
         captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight 
        } 
    
         // and so on for other orientations 
    
        return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight)); 
    } 
    
+2

一直在尋找一個解決方案,爲年齡。謝謝! –

+2

+1爲captureVideoPreviewLayer.connection.videoOrientation。謝謝。 – iLearner

相關問題