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

时间:2017-02-20 15:21:04

标签: ios swift3 avfoundation avcapturesession

所以,我正试图在我的应用程序中使用手机上的相机,但我很成功,但不幸的是,在使用预览时,图层没有填满整个屏幕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)
    }

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

请尝试以下方法:

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

答案 2 :(得分:0)

尝试以下代码:

 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 Doc: AVFoundation class image