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

时间:2022-06-28 21:08:45

I'm basically after the answer to this question AVCaptureVideoPreviewLayer orientation - need landscape except in Swift.

我基本上是在回答这个问题AVCaptureVideoPreviewLayer方向 - 需要景观除了在Swift。

I'm targeting iOS 8 and AVCaptureVideoPreviewLayer doesn't seem to have a setVideoOrientation function.

我的目标是iOS 8,而AVCaptureVideoPreviewLayer似乎没有setVideoOrientation功能。

How should I be detecting that the orientation has changed, and rotating AVCaptureVideoPreviewLayer appropriately?

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

3 个解决方案

#1


16  

Detect and apply it at viewWillLayoutSubviews.

在viewWillLayoutSubviews中检测并应用它。

Here's how:

override func viewWillLayoutSubviews() {

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

    switch (orientation) {
    case .Portrait:
        previewLayer?.connection.videoOrientation = .Portrait
    case .LandscapeRight:
        previewLayer?.connection.videoOrientation = .LandscapeLeft
    case .LandscapeLeft:
        previewLayer?.connection.videoOrientation = .LandscapeRight
    default:
        previewLayer?.connection.videoOrientation = .Portrait
    }
}

#2


0  

update for Swift 3

更新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
    }
}

#3


0  

For me I need it to change orientation when I rotate iPad left or right, the solution that helped me is:

对我来说,当我向左或向右旋转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
    }
}

#1


16  

Detect and apply it at viewWillLayoutSubviews.

在viewWillLayoutSubviews中检测并应用它。

Here's how:

override func viewWillLayoutSubviews() {

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

    switch (orientation) {
    case .Portrait:
        previewLayer?.connection.videoOrientation = .Portrait
    case .LandscapeRight:
        previewLayer?.connection.videoOrientation = .LandscapeLeft
    case .LandscapeLeft:
        previewLayer?.connection.videoOrientation = .LandscapeRight
    default:
        previewLayer?.connection.videoOrientation = .Portrait
    }
}

#2


0  

update for Swift 3

更新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
    }
}

#3


0  

For me I need it to change orientation when I rotate iPad left or right, the solution that helped me is:

对我来说,当我向左或向右旋转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
    }
}