Swift Camera App-启用左右横向模式

时间:2019-11-04 12:31:57

标签: swift xcode camera avfoundation landscape

我是Swift的新手,我尝试以横向模式(左右)实现相机应用。 我尝试了一个视频教程,该相机应用程序基本上可以运行,但是只能在人像模式下使用(不是横向)。 如果我在项目中启用了左+右横向(并且我禁用了肖像模式),则相机将如下所示:

enter image description here

我在做什么错?我发现了一些有关同一问题的帖子,但都没有帮助我解决问题。

最好的问候

我尝试了仿射变换,例如CGAffineTransform(rotationAngle:-90),但这也不能解决问题。


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

    }

1 个答案:

答案 0 :(得分:0)

尝试一下:

private var previewLayer: AVCaptureVideoPreviewLayer?

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