AVCaptureVideoPreviewLayer平滑方向旋转 [英] AVCaptureVideoPreviewLayer smooth orientation rotation

查看:135
本文介绍了AVCaptureVideoPreviewLayer平滑方向旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试禁用任何可辨别的方向旋转到AVCaptureVideoPreviewLayer,同时仍保持任何子视图的旋转。 AVCaptureVideoPreviewLayer确实具有方向属性,更改它确实允许图层正确显示任何方向。然而,旋转涉及AVCaptureVideoPreviewLayer的一些时髦旋转,而不是像在相机应用程序中那样保持平滑。

I'm trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. AVCaptureVideoPreviewLayer does have an orientation property, and changing it does allow for the layer to display properly for any orientation. However, the rotation involves some funky rotation of the AVCaptureVideoPreviewLayer, rather than staying smooth as it does in the Camera app.

这就是我如何正确工作的方向,减去轮换中的故障:

This is how I've gotten orientation to work properly, minus the hitch in the rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    _captureVideoPreviewLayer.frame = self.view.bounds;
    _captureVideoPreviewLayer.orientation = [[UIDevice currentDevice] orientation];
}

如何让这一层像Camera应用程序一样,同时保持旋转对于它的子视图?

How do I get this layer to act like the Camera app, while maintaining rotations for its subviews?

另外,我已经看到AVCaptureVideoPreviewLayer上的orientation属性已被折旧,而AVCaptureConnection的videoOrientation属性应该被使用,但是我不认为我有AVCaptureConnection可以在这里访问(我只是在屏幕上显示相机)。如何设置它以访问videoOrientation?

Also, as an aside, I've seen that the orientation property is depreciated on AVCaptureVideoPreviewLayer, and the videoOrientation property of AVCaptureConnection should be used instead, but I don't think I have an AVCaptureConnection to access here (I'm simply displaying the camera on the screen). How should I set this up to access videoOrientation?

推荐答案

在包含预览图层的视图上使用仿射变换可创建平滑过渡:

Using an affine transform on the view containing the preview layer creates a smooth transition:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (cameraPreview) {
        if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
            cameraPreview.transform = CGAffineTransformMakeRotation(0);
        } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
            cameraPreview.transform = CGAffineTransformMakeRotation(M_PI/2);
        } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
            cameraPreview.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }
        cameraPreview.frame = self.view.bounds;
    }
}

willAnimateRotationToInterfaceOrientation 在旋转动画块中调用函数,因此在此处更改属性将与其余旋转动画一起生成动画。此功能已在iOS 6中逐步淘汰,并替换为处理设备旋转的新方法,因此现在可以使用,但不是最佳解决方案。

The willAnimateRotationToInterfaceOrientation function is called within the rotation animation block so changing properties here will animate along with the rest of the rotation animations. This function has been phased out in iOS 6 and replaced with new methods to handle device rotation, so this works for now, but isn't the best solution.

这篇关于AVCaptureVideoPreviewLayer平滑方向旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆