iOS:相机方向

A.V*_*ila 12 iphone camera objective-c rotation avcapturesession

我想使用AVCaptureSession使用相机拍摄图像.

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

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

我执行:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ 
return UIInterfaceOrientationIsLandscapse(interfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)

当我旋转设备时,它会将应用程序从横向左侧旋转到横向右侧,反之亦然,但是当我在横向左侧时,我只能正确地看到相机.当应用程序处于横向右侧时,视频将旋转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'
Run Code Online (Sandbox Code Playgroud)

此行中发生错误:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;
Run Code Online (Sandbox Code Playgroud)

我把它放在了shouldAutorotateToInterfaceOrientation方法里面.你知道这个错误的原因是什么吗?

谢谢

Kha*_*azi 24

默认的摄像头方向是奇怪的UIInterfaceOrientationLeft.

相机方向不随设备的旋转而改变.它们是分开的.您必须手动调整相机方向:

将以下内容放在您传递给InterfaceOrientation的方法中(也许您可以从上面的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @ A.Vila,您在问题中提到了相机方向而不是预览图层方向.仅供将来参考,AVCaptureSession创建2个独立的连接(用于预览图层和相机输出),您必须单独旋转!我更新了上面预览图层方向的答案. (2认同)