AVCaptureSession 和预览层不填满整个屏幕

AVCaptureSession and Preview Layer does not fill entire screen

所以,我尝试在我的应用程序中使用 phone 上的摄像头,我成功了,但不幸的是,在使用预览时,图层没有填满整个屏幕 Here is an image to show you what it looks like

这是我的代码:

if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
        self.previewLayer = previewLayer
        self.view.layer.addSublayer(self.previewLayer)
        self.previewLayer.frame = self.view.layer.frame
        captureSession.startRunning()

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)]

        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureSession.canAddOutput(dataOutput) {
            captureSession.addOutput(dataOutput)
        }

        captureSession.commitConfiguration()


        let queue = DispatchQueue(label: "com.Osmo.captureQueue")
        dataOutput.setSampleBufferDelegate(self, queue: queue)
    }

请尝试以下操作:

if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
        self.previewLayer = previewLayer
        self.previewLayer.frame = self.view.bounds
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.view.layer.insertSublayer(self.previewLayer, at: 0)
        captureSession.startRunning()

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)]

        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureSession.canAddOutput(dataOutput) {
            captureSession.addOutput(dataOutput)
        }

        captureSession.commitConfiguration()


        let queue = DispatchQueue(label: "com.Osmo.captureQueue")
        dataOutput.setSampleBufferDelegate(self, queue: queue)
    }

尝试以下代码:

 if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
                    previewLayer.bounds = view.bounds
                    previewLayer.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)   //CGPointMake(view.bounds.midX, view.bounds.midY)
                    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                    let cameraPreview = UIView(frame: CGRect(x: 0.0, y: 0.0, width: view.bounds.width, height: view.bounds.height));
                    cameraPreview.tag = 44221007
                    cameraPreview.layer.addSublayer(previewLayer)
                    //cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector(("saveToCamera:"))))
                    view.addSubview(cameraPreview)
                    self.view.sendSubview(toBack: cameraPreview);

                }

Apple 文档:

这可能是因为您试图从具有自动布局约束的视图中设置预览图层的框架。尝试向您的视图控制器添加 viewDidLayoutSubviews 方法的覆盖:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.previewLayer.frame = self.view.layer.bounds
}