且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS:相机方向

更新时间:2023-02-02 14:59:29

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