我想使用 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 方法中。 你知道这个错误的原因是什么吗?
谢谢
最佳答案
默认的相机方向很奇怪 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;
}
}
关于iphone - iOS : camera orientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811641/