允许 AVCaptureVideoPreviewLayer 预览随设备旋转

Allow AVCaptureVideoPreviewLayer preview rotate with device

我的应用程序中有一个 AVCaptureVideoPreviewLayer,它在纵向设备上运行良好,但在横向设备上,视频是横向的。 我试过了

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
}

但问题依然存在。

我修好了 - 我只需要添加这个 -

    if UIDevice.current.orientation == .portrait {
        previewLayer.connection?.videoOrientation = .portrait
    } else if UIDevice.current.orientation == .portraitUpsideDown {
        previewLayer.connection?.videoOrientation = .portraitUpsideDown
    } else if UIDevice.current.orientation == .landscapeLeft {
        previewLayer.connection?.videoOrientation = .landscapeRight
    } else if UIDevice.current.orientation == .landscapeRight {
        previewLayer.connection?.videoOrientation = .landscapeLeft
    }

-当设备旋转时被调用。我还在初始化 AVCaptureVideoPreviewLayer.

后在启动时调用它