3、自定义相机预览界面旋转问题

背景:开发中涉及到使用自定义相机预览界面,且在该界面需要将单页面设置成横屏模式,本项目中指定设置为:UIInterfaceOrientationLandscapeRight
问题:自定义相机预览界面开发好后,在单页面横屏时,会自动右旋90°
解决方案:

- (void)viewWillLayoutSubviews { 
    _captureVideoPreviewLayer.frame = self.view.bounds; 
    if (_captureVideoPreviewLayer.connection.supportsVideoOrientation) { 
        //如果时指定的横屏模式,直接赋值即可;比如我直接赋值AVCaptureVideoOrientationLandscapeRight;不用读取设备横屏模式
        _captureVideoPreviewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation]; 
    } 
} 
- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation { 
    switch (orientation) { 
        case UIInterfaceOrientationPortrait: 
            return AVCaptureVideoOrientationPortrait; 
        case UIInterfaceOrientationPortraitUpsideDown: 
            return AVCaptureVideoOrientationPortraitUpsideDown; 
        case UIInterfaceOrientationLandscapeLeft: 
            return AVCaptureVideoOrientationLandscapeLeft; 
        case UIInterfaceOrientationLandscapeRight: 
            return AVCaptureVideoOrientationLandscapeRight; 
        default: 
            break; 
        } 
    NSLog(@"Warning - Didn't recognise interface orientation (%d)",orientation); 
    return AVCaptureVideoOrientationPortrait; 
   } 

问题原因:CALayer不支持自动旋转,因此,不像UIView你添加作为子视图,它不会旋转时,其父UIView旋转。 因此,每当父视图的边界发生更改( 不是父视图的帧,因为帧在旋转后保持不变 )时,您必须手动更新其框架。 这是通过覆盖容器视图控制器中的viewWillLayoutSubviews来实现的。
其次,你应该使用'videoOrientation'属性来告知AVFoundation有关的方向,以便正确预览。

参考链接:https://ios.dovov.com/avcapturevideopreviewlayer-6.html

答案截图

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容