2013-02-11 14 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奇妙なことです。

カメラの向きは、デバイスの回転によって変化しません。彼らは別々です。手動でカメラの向きを調整する必要があります。

は、あなたが(多分あなたは、デバイスが回転するように、上記shouldAutorotateToInterfaceOrientationからそれを呼び出すと、カメラが回転)にtoInterfaceOrientationを渡す方法で次のように入れてください:

あなたがする必要がありますプレビューレイヤ接続を先に取得

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は推奨されていないので、良い解決策ではありません。ありがとう、 –

+2

@ A.Vila、質問では、プレビューのレイヤの方向ではなく、カメラの方向を述べた。今後の参照のために、AVCaptureSessionは2つの別々の接続(プレビューレイヤーとカメラ出力用)を作成し、両方を別々に回転させる必要があります。私は上記のプレビューレイヤオリエンテーションの答えを更新しました。 – Spectravideo328

+0

あなたの答えをありがとう。私はそれを試しましたが、私はエラーがあります。質問をエラー情報で更新します。あなたはその問題が何であるか知っていますか?どうもありがとう。 –

関連する問題