AVCaptureVideoPreviewLayer的

AVCaptureVideoPreviewLayer的

我很难改变AVCaptureVideoPreviewLayer的界限。无论我做什么,它总是以小尺寸结束,如下所示:
ios - 无法设置AVCaptureVideoPreviewLayer的边界-LMLPHP
这是我的代码:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.imageLayer.bounds = CGRect(x: 0, y: 0, width: 375, height: 100)
    }

    private func startLiveVideo() {

        session.sessionPreset = AVCaptureSession.Preset.photo
        let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

        let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)
        let deviceOutput = AVCaptureVideoDataOutput()
        deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
        deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.default))
        session.addInput(deviceInput)
        session.addOutput(deviceOutput)

        self.imageLayer = AVCaptureVideoPreviewLayer(session: session)
        imageLayer.frame = CGRect(x: 0, y: 0, width: 375, height: 100)
        self.imageView.layer.addSublayer(self.imageLayer)

        session.startRunning()
    }

解决方案:
self.imageLayer.videoGravity = .resizeAspectFill

最佳答案

您可以首先从情节提要或通过viewcontroller 500 x 500或您想要的任何大小指定imageView的宽度和高度。

imageLayer.frame = CGRect(x: 0, y: 0, width: self.imageView.frame.width, height: self.imageView.frame.height)

self.imageView.layer.addSublayer(self.imageLayer)

关于ios - 无法设置AVCaptureVideoPreviewLayer的边界,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50279227/

10-10 21:14