【问题标题】:iOS: camera orientationiOS:相机方向
【发布时间】:2013-01-26 12:36:17
【问题描述】:

我想使用 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 方法里面。 你知道这个错误可能是什么原因吗?

谢谢

【问题讨论】:

    标签: iphone objective-c camera rotation avcapturesession


    【解决方案1】:

    奇怪的是,默认的相机方向是 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;
        }
    }
    

    【讨论】:

    • 这个解决方案对我不起作用。它什么也没做...我使用 [self.prevLayer setOrientation:[[UIDevice currentDevice]orientation]] 解决了这个问题。但是 setOrientation 已被弃用,因此它不是一个好的解决方案。谢谢
    • @A.Vila,您在问题中提到的是相机方向而不是预览层方向。仅供将来参考,AVCaptureSession 创建了 2 个单独的连接(用于预览层和相机输出),您必须分别旋转两者!。我更新了上面预览层方向的答案。
    • 感谢您的回答。我已经尝试过了,但我有一个错误。我用错误信息更新问题。你知道问题是什么吗?非常感谢。
    • 在 ios 6 上运行正常,谢谢。对于早期版本,此代码可能很有用: if ([self.prevLayer isOrientationSupported]){ self.prevLayer.orientation = newOrientation;}
    • 此代码适用于 iOS7 和 8.1。谢谢! // 我使用 UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation]; 因为不推荐使用 shouldAutorotateToInterfaceOrientation..
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多