2013-02-11 90 views
12

我想使用AVCaptureSession使用相機捕捉圖像。iOS:相機方向

它工作正常,我啓動相機,我可以得到輸出。但是,當我旋轉設備時,我在視頻方向上遇到了一些問題。

首先,我想支持橫向左右方向,並且可能過於晚於肖像模式。

我實現:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ 
return UIInterfaceOrientationIsLandscapse(interfaceOrientation); 
} 

當我旋轉設備,它從橫向左向右橫向或反之旋轉的應用程序,但我只看到正確的攝像頭,當我在左邊的景觀。當應用處於右側橫向時,視頻旋轉180度。

非常感謝。

更新:

我試過Spectravideo328的答案,但我有一個錯誤,當我嘗試旋轉設備和應用程序崩潰。這是錯誤:

[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210 

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210' 

在此行中出現的錯誤:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection; 

我把它shouldAutorotateToInterfaceOrientation方法內。 你知道這個錯誤的原因是什麼?

感謝

回答

23

默認攝像頭的方向是奇怪的是UIInterfaceOrientationLeft。

相機方向不隨設備的旋轉而改變。他們是分開的。你必須手動調整鏡頭方向:

放入你傳遞toInterfaceOrientation的方法如下(也許你從shouldAutorotateToInterfaceOrientation調用它上面,從而使設備旋轉,攝像頭轉動):

你必須首先獲得預覽圖層連接

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection; 

if ([previewLayerConnection isVideoOrientationSupported]) 
{ 
    switch (toInterfaceOrientation) 
    { 
     case UIInterfaceOrientationPortrait: 
      [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; //home button on left. Refer to .h not doc 
      break; 
     default: 
      [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; //for portrait upside down. Refer to .h not doc 
      break; 
    } 
} 
+0

此解決方案對我無效。它沒有做任何事情......我使用[self.prevLayer setOrientation:[[UIDevice currentDevice] orientation]]解決了這個問題。但setOrientation已被棄用,所以它不是一個好的解決方案。謝謝 – 2013-02-13 17:41:29

+2

@ A.Vila,你在問題中提到了相機的方向,而不是預覽層的方向。僅供將來參考,AVCaptureSession會創建2個單獨的連接(用於預覽圖層和攝像機輸出),您將不得不單獨旋轉!我更新了我上面預覽圖層方向的答案。 – Spectravideo328 2013-02-13 18:05:38

+0

感謝您的回答。我試過了,但是我有一個錯誤。我更新錯誤信息的問題。你知道問題是什麼嗎?非常感謝。 – 2013-05-09 16:20:43