AVFoundation orientation issues



Hi, I’m creating a QRcode scanner. I already implemented the camera feed but the orientation is wrong. See I’m deploying it to iPad devices using landscape orientation only but the feed is in portrait mode and regardless of the videoOrientation value I set to AVCaptureVideoPreviewLayer it doesn’t work. I’m using swift 4 (xcode9:iOS11)


I have experienced this one before. Can you show the code where you initialize your camera configurations? The one where the setting of videoOrientation is at. Have you can try this one:

previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.landscapeRight


Thanks for the reply.

I’m already setting the orientation through previewLayer.connection.videoOrientation but still the feed is sort of in portrait mode. I did created a similar app way back using ObjC but the device is in portrait mode.

func startCamera() {
captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

do {
  let input = try AVCaptureDeviceInput(device: captureDevice!)
  let captureSession = AVCaptureSession()
  preview = AVCaptureVideoPreviewLayer(session: captureSession)
  preview.videoGravity = AVLayerVideoGravity.resizeAspectFill
  preview.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeRight
  preview.frame.size = previewView.frame.size
  previewView.layer.insertSublayer(preview, at: 0)

} catch {



I was able to fix it by wrapping previewLayer.connection … within DispatchQueue.main.async {…}