AVCaptureVideoPreviewLayer横向定位

时间:2014-01-21 12:37:42

标签: ios avfoundation calayer landscape

如何让'AVCaptureVideoPreviewLayer'在横向方向正确显示?它在纵向上工作正常,但不会旋转,并在父视图控制器处于横向时显示旋转的相机捕获。

7 个答案:

答案 0 :(得分:30)

首先,答案

- (void)viewWillLayoutSubviews {
  _captureVideoPreviewLayer.frame = self.view.bounds;
  if (_captureVideoPreviewLayer.connection.supportsVideoOrientation) {
    _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;
}

我在这个问题上遇到过几个SO帖子,找不到任何简单的解释,所以我想我会分享自己的。

如果您在此问题上关注Apple的示例,当您将iOS设备旋转到横向时,您将遇到两个潜在的问题

  1. 捕捉将显示为旋转(就像您将相机保持90度关闭一样)
  2. 它不会完全跨越其设定范围
  3. 这里的问题是'CALayer'不支持自动旋转,因此,与你添加为子视图的'UIView'不同,当它的父'UIView'旋转时它不会旋转。因此,每当父视图的边界发生变化时,您必须手动更新其框架(不是父视图的框架,因为框架在旋转后保持相同)。这是通过覆盖容器视图控制器中的“viewWillLayoutSubviews”来实现的。

    其次,你应该使用'videoOrientation'属性来告知AVFoundation方向,以便正确进行预览。

    希望这有帮助。

答案 1 :(得分:7)

Swift3

func updateVideoOrientation() {
    guard let previewLayer = self.previewLayer else {
        return
    }
    guard previewLayer.connection.isVideoOrientationSupported else {
        print("isVideoOrientationSupported is false")
        return
    }

    let statusBarOrientation = UIApplication.shared.statusBarOrientation
    let videoOrientation: AVCaptureVideoOrientation = statusBarOrientation.videoOrientation ?? .portrait

    if previewLayer.connection.videoOrientation == videoOrientation {
        print("no change to videoOrientation")
        return
    }

    previewLayer.frame = cameraView.bounds
    previewLayer.connection.videoOrientation = videoOrientation
    previewLayer.removeAllAnimations()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: nil, completion: { [weak self] (context) in
        DispatchQueue.main.async(execute: {
            self?.updateVideoOrientation()
        })
    })
}
extension UIInterfaceOrientation {
    var videoOrientation: AVCaptureVideoOrientation? {
        switch self {
        case .portraitUpsideDown: return .portraitUpsideDown
        case .landscapeRight: return .landscapeRight
        case .landscapeLeft: return .landscapeLeft
        case .portrait: return .portrait
        default: return nil
        }
    }
}

答案 2 :(得分:2)

override func viewWillLayoutSubviews() {
    self.previewLayer.frame = self.view.bounds
    if previewLayer.connection.isVideoOrientationSupported {
        self.previewLayer.connection.videoOrientation = self.interfaceOrientation(toVideoOrientation: UIApplication.shared.statusBarOrientation)
    }
}

func interfaceOrientation(toVideoOrientation orientation: UIInterfaceOrientation) -> AVCaptureVideoOrientation {
    switch orientation {
    case .portrait:
        return .portrait
    case .portraitUpsideDown:
        return .portraitUpsideDown
    case .landscapeLeft:
        return .landscapeLeft
    case .landscapeRight:
        return .landscapeRight
    default:
        break
    }

    print("Warning - Didn't recognise interface orientation (\(orientation))")
    return .portrait
}

// SWIFT 3 CONVERSION

答案 3 :(得分:0)

除了v​​iewWillLayoutSubviews()之外,还必须实现didRotateFromInterfaceOrientation()。

这是因为如果设备从纵向模式旋转到纵向倒置模式,子视图布局将不会更新(显然出于效率原因)。

答案 4 :(得分:0)

Swift 5(iOS 13.0 +):

    func updateVideoOrientation() {
        guard let videoPreviewLayer = self.videoPreviewLayer else {
            return
        }
        guard videoPreviewLayer.connection!.isVideoOrientationSupported else {
            print("isVideoOrientationSupported is false")
            return
        }
        let statusBarOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
        let videoOrientation: AVCaptureVideoOrientation = statusBarOrientation?.videoOrientation ?? .portrait
        videoPreviewLayer.frame = view.layer.bounds
        videoPreviewLayer.connection?.videoOrientation = videoOrientation
        videoPreviewLayer.removeAllAnimations()
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)

        coordinator.animate(alongsideTransition: nil, completion: { [weak self] (context) in
            DispatchQueue.main.async(execute: {
                self?.updateVideoOrientation()
            })
        })
    }

extension UIInterfaceOrientation {
    var videoOrientation: AVCaptureVideoOrientation? {
        switch self {
        case .portraitUpsideDown: return .portraitUpsideDown
        case .landscapeRight: return .landscapeRight
        case .landscapeLeft: return .landscapeLeft
        case .portrait: return .portrait
        default: return nil
        }
    }
}

答案 5 :(得分:0)

Jacksanford和NeonEye的答案很好。但是,我有个建议。

由于statusBarOrientation在iOS 13中已过时,我最终使用了

connection.videoOrientation = self.interfaceOrientation(toVideoOrientation: UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .portrait

答案 6 :(得分:0)

仅更改AVCaptureVideoPreviewLayer的连接方向还不够。 这只会更新您在屏幕上看到的图像。

但是实际图像仍将保持不变。您可以以不同的方向打印输出图像尺寸并检查结果。

要使旋转真正起作用,您还需要更改AVCaptureSession的连接方向。

func updateVideoOrientation() {
    if let previewLayerConnection = previewLayer.connection, previewLayerConnection.isVideoOrientationSupported {
        previewLayerConnection.videoOrientation = currentVideoOrientation
    }
    if let captureSessionConnection = captureSession.connections.first, captureSessionConnection.isVideoOrientationSupported {
        captureSessionConnection.videoOrientation = currentVideoOrientation
    }
}