Gymknopf
Gymknopf

Reputation: 31

Swift Camera App - enable landscape mode right / left

I am new in Swift and I try to implement a camera app in landscape mode (left and right). I tried a video tutorial, the camera app basically works but just in portrait mode (not landscape). If I enable landscape left + right in the project (and I disenable portrait mode), the camera looks like this:

enter image description here

What am I doing wrong? I found some posts about the same problem, but none of them helped me to solve the problem.

Best Regards

I tried affine transformation like CGAffineTransform(rotationAngle: -90) but this also doesn't solve the problem.


import UIKit
import AVFoundation

class ViewController: UIViewController {


    @IBOutlet weak var cameraView: UIView!

    var captureSession: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)



    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 10.2, *)
        {
            let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
            do
            {
                let input = try AVCaptureDeviceInput(device: captureDevice!)
                captureSession = AVCaptureSession()
                captureSession?.addInput(input)
                videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
                videoPreviewLayer?.frame = view.layer.bounds
                cameraView.layer.addSublayer(videoPreviewLayer!)
                captureSession?.startRunning()
            }
            catch
            {
                print("error")
            }
        }

    }

Upvotes: 2

Views: 905

Answers (1)

Pavan Gudur
Pavan Gudur

Reputation: 37

Try this:

private var previewLayer: AVCaptureVideoPreviewLayer?

previewLayer?.connection?.videoOrientation = .landscapeLeft
previewLayer?.connection?.videoOrientation = .portrait

Upvotes: 1

Related Questions