首页 文章

iOS:相机方向

提问于
浏览
12

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

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

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

我执行:

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

当我旋转设备时,它会将应用程序从横向左侧旋转到横向右侧,反之亦然,但是当我在横向左侧时,我只能正确地看到相机 . 当应用程序处于横向右侧时,视频将旋转180度 .

非常感谢你 .

Update:

我已经尝试过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方法里面 . 你知道这个错误的原因是什么吗?

谢谢

1 回答

  • 23

    默认的摄像头方向是奇怪的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;
        }
    }
    

相关问题