How can I detect and apply landscape orientation to AVCaptureVideoPreviewLayer in Swift?

avcapturevideopreviewlayer portrait
avcapturevideodataoutput rotated
avcapturevideodataoutput image orientation
avcapturevideodataoutput orientation
swift camera rotation
cmsamplebuffer orientation
didoutputsamplebuffer orientation
change video orientation swift 3

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

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

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

Detect and apply it at 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
    }
}

orientation, Use videoOrientation ( AVCaptureConnection ) instead. Availability. iOS 4.0–6.0 Deprecated; Mac Catalyst 13.0–13.0 Deprecated. Framework. AVFoundation. Thanks!! This worked great for me on an iOS9 app with Swift 2.2 to force portrait. I started with a 'parent' view in landscape, put the above code in viewDidLoad of the ViewController which i present modally from the 'parent', and when the modal view was presented, the view rotated to portrait.

update for 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
    }
}

I'm stuck: How to get the same rot…, Apple Developer � Discover � Design � Develop � Distribute � Support � Account Which kind of „magic“ does AVCaptureVideoPreviewLayer use to prevent this I the iOS Camera App and the developer example AVCamManual the video preview appear in portrait mode, even if the user is holding the phone in landscape. AVCapture Video Preview Layer is a subclass of CALayer that you use to display video as it’s captured by an input device.. You use this preview layer in conjunction with a capture session, as shown in the following code fragment.

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

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

AVCaptureVideoPreviewLayer orientation, avcapturevideopreviewlayer swift 4 swift detect landscape orientation preview layer changes when the screen rotates (you cannot use frame of the root view� A preview layer begins previewing when you call the capture session’s start Running method. When associated with an AVCapture Multi Cam Session, all video preview layers are guaranteed to be previewing by the time the blocking call to start Running or commit Configuration returns.

Xamarin Solution in ViewController;

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;
        }
    }

How can I detect and apply landscape orientation to , I'm basically after the answer to this question AVCaptureVideoPreviewLayer orientation - need landscape except in Swift. 我基本上是在回答� However, the camera display appears as a portrait rectangle in the middle of the landscape screen, and the orientation of the camera preview image is wrong by 90 degrees. What am I doing wrong? I need the camera preview layer to appear in the full screen, in landscape mode, and the image should be orientated correctly.

another solution in Swift 4 it would be using Notification.

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

UIDevice.orientationDidChangeNotification, object: nil)

and in selector function you can check the orientation:

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

Remember to remove observer when is not necessary.

Very IMPORTANT: your physical iPhone must NOT have lock on rotation (I've lost 2 hour for this because in this case, the method are not intercepted :) ).

swift - 如何在Swift中检测横向方位并将其应用于 , How can I detect and apply landscape orientation to AVCaptureVideoPreviewLayer in Swift?. I'm basically after the answer to this question� Overview. AVCapture Video Preview Layer is a subclass of CALayer that you use to display video as it’s captured by an input device.. You use this preview layer in conjunction with a capture session, as shown in the following code fragment.

SOLVED: UITraitCollection: How to detect device orientation , iOS – Hacking with Swift forums. How can I determine the iPad being in landscape or portrait ? Still a question: traitCollection is available on a viewController, but I want to use it more generic and reusable in more place,� If you just want to read the current interface orientation you can do so using UIApplication.shared.statusBarOrientation, which will contain a value such as .portraitUpsideDown. Alternatively, you can use its isLandscape and isPortrait properties to check for both values of landscape or both values of portrait at once.

如何在Swift中检测并应用横向方向到AVCaptureVideoPreviewLayer?, 我基本上是在回答这个问题AVCaptureVideoPreviewLayer方向除了在Swift中需要 I detect and apply landscape orientation to AVCaptureVideoPreviewLayer in� ⚠️Device Orientation != Interface Orientation⚠️ Swift 5.* iOS13 and below. You should really make a difference between: Device Orientation => Indicates the orientation of the physical device; Interface Orientation => Indicates the orientation of the interface displayed on the screen

iOS: Camera video preview layer should not rotate with orientation , When you have a landscape cordova app and start scanning the camera is not rotated correctly. The AVCaptureVideoOrientation should� A Boolean value that indicates whether the layer is rendering video frames from its source. Developer

Comments
  • thankyou sir... you saved my day... add this line before break will help previewLayer.frame = self.view.bounds
  • This needs to be updated for the latest Swift. Take note that the videoOrientation is flipped from the device orientation for landscape modes.