12

我想使用 AVCaptureSession 用相机捕捉图像。

它工作正常,我启动相机,我可以得到输出。但是,当我旋转设备时,我遇到了一些视频方向问题。

首先,我想支持横向左右方向,以后也可能是纵向模式。

我实现:

- (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 方法里面。你知道这个错误的原因是什么吗?

谢谢

4

1 回答 1

24

奇怪的是,默认的相机方向是 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;
    }
}
于 2013-02-11T12:34:31.767 回答