2013-02-11 66 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