如何在Swift中检测并应用横屏方向到AVCaptureVideoPreviewLayer?

8

我基本上是在寻找这个问题的答案:AVCaptureVideoPreviewLayer方向 - 需要横屏,但是用Swift。

我正在针对iOS 8进行开发,但是AVCaptureVideoPreviewLayer似乎没有setVideoOrientation函数。

我应该如何检测方向已更改,并适当旋转AVCaptureVideoPreviewLayer?

5个回答

24

viewWillLayoutSubviews中检测并应用它,或者如果您正在使用layoutSubviews的UIView子类,则可以这样做。

操作方法如下:

override func viewWillLayoutSubviews() {
                
    let orientation: UIDeviceOrientation = UIDevice.current.orientation

    previewLayer.connection?.videoOrientation = {
        switch (orientation) {
        case .portrait:
            return .portrait
        case .landscapeRight:
            return .landscapeLeft
        case .landscapeLeft:
            return .landscapeRight
        default:
            return .portrait
        }
    }()
}

谢谢您,先生……您救了我的一天。 在break之前添加这行代码会有所帮助: previewLayer.frame = self.view.bounds - Arpan Sharma
1
这需要更新到最新的Swift版本。请注意,在横向模式下,videoOrientation与设备方向相反。 - Jeff Muir

1

更新至Swift 3

  override func viewWillLayoutSubviews() {

    let orientation: UIDeviceOrientation = UIDevice.current.orientation
    print(orientation)

    switch (orientation) {
    case .portrait:
        videoPreviewLayer?.connection.videoOrientation = .portrait
    case .landscapeRight:
        videoPreviewLayer?.connection.videoOrientation = .landscapeLeft
    case .landscapeLeft:
        videoPreviewLayer?.connection.videoOrientation = .landscapeRight
    case .portraitUpsideDown:
            videoPreviewLayer?.connection.videoOrientation = .portraitUpsideDown
    default:
        videoPreviewLayer?.connection.videoOrientation = .portraitUpsideDown
    }
}

0

在ViewController中的Xamarin解决方案;

public override void ViewWillLayoutSubviews()
    {
        base.ViewWillLayoutSubviews();

        var videoPreviewLayerConnection = PreviewView.VideoPreviewLayer.Connection;
        if (videoPreviewLayerConnection != null)
        {
            var deviceOrientation = UIDevice.CurrentDevice.Orientation;
            if (!deviceOrientation.IsPortrait() && !deviceOrientation.IsLandscape())
                return;

            var newVideoOrientation = VideoOrientationFor(deviceOrientation);
            var oldSize = View.Frame.Size;
            var oldVideoOrientation = videoPreviewLayerConnection.VideoOrientation;
            videoPreviewLayerConnection.VideoOrientation = newVideoOrientation;
        }
    }

0

对我来说,我需要在我将iPad向左或向右旋转时改变方向,帮助我的解决方案是:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    let orientation = UIDevice.current.orientation

    switch (orientation) {
    case .portrait:
        videoPreviewLayer?.connection.videoOrientation = .portrait
    case .landscapeLeft:
        videoPreviewLayer?.connection.videoOrientation = .landscapeRight
    case .landscapeRight:
        videoPreviewLayer?.connection.videoOrientation = .landscapeLeft
    default:
        videoPreviewLayer?.connection.videoOrientation = .portrait
    }
}

0

在 Swift 4 中,另一个解决方案是使用通知。

        NotificationCenter.default.addObserver(self,
                                           selector: #selector(detected),
                                           name: 

UIDevice.orientationDidChangeNotification,object: nil)

在选择器函数中,您可以检查方向:

let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch orientation {
    case .portrait:
         stuff()
    case .landscapeLeft:
         stuffOther()
    case .landscapeRight
         stuffOther()
    default:
         stuffOther()
}

记得在不需要时删除观察者。

非常重要:你的物理iPhone不能有旋转锁定(我因为这个浪费了2个小时,因为在这种情况下,方法不会被拦截 :))。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接