iOS:相机方向 [英] iOS: camera orientation
问题描述
我想使用 AVCaptureSession 用相机捕捉图像.
I want to capture an image with the camera using AVCaptureSession.
它工作正常,我启动相机,我可以得到输出.但是,我在旋转设备时遇到了一些视频方向问题.
It works ok, I start the camera and I can get the output. However, I have some problems with video orientation when I rotate the device.
首先,我想支持横向左右方向,以后也可能是纵向模式.
First, I want to support landscape left and right orientations and may be portrait modes too later.
我执行:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{
return UIInterfaceOrientationIsLandscapse(interfaceOrientation);
}
当我旋转设备时,它会将应用程序从左侧横向旋转到右侧,反之亦然,但我只有在左侧横向时才能正确看到相机.当应用处于横向右侧时,视频会旋转 180 度.
When I rotate the device, it rotates the app from landscape left to landscape right or vice versa, but I only see the camera correctly when I'm on landscape left. When the app is on landscape right, the video is rotated by 180 degrees.
非常感谢.
更新:
我已经尝试了 Spectravideo328 的答案,但是当我尝试旋转设备并且应用程序崩溃时出现错误.这是错误:
I've tried Spectravideo328 answer but I have an error when I try to rotate the device and the app crash. This is the error:
[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 方法里面.你知道这个错误可能是什么原因吗?
I put it inside shouldAutorotateToInterfaceOrientation method. Do you know what could be the reason of this error?
谢谢
推荐答案
奇怪的是,默认的相机方向是 UIInterfaceOrientationLeft.
The default camera orientation is oddly enough UIInterfaceOrientationLeft.
相机方向不会随着设备的旋转而改变.他们是分开的.您必须手动调整相机方向:
The camera orientation does not change with the rotation of the device. They are separate. You have to adjust the camera orientation manually:
将以下内容放入您传递给 toInterfaceOrientation 的方法中(也许您从上面的 shouldAutorotateToInterfaceOrientation 调用它,以便设备旋转和相机旋转):
Put the following in a method that you pass toInterfaceOrientation to (maybe you call it from shouldAutorotateToInterfaceOrientation above so that the device rotates and the camera rotates):
你必须先获得预览层连接
You have to get the preview Layer connection first
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;
}
}
这篇关于iOS:相机方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!