使用IOS相机预览图层填充视图

时间:2017-07-14 00:12:38

标签: ios swift

完成教程,我试图进行全屏预览。目前,我的相机看起来非常方形,我相信这可能是一个方面适合的问题。相机似乎打到左右两侧。如何将我的预览固定在导航栏底部和屏幕底部以及两侧?

import UIKit
import AVFoundation

class HomeViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    let captureSession = AVCaptureSession()
    var previewLayer:CALayer!

    var captureDevice:AVCaptureDevice!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        prepareCamera()
    }

    func prepareCamera(){
        captureSession.sessionPreset = AVCaptureSessionPresetPhoto

        if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back).devices {
            captureDevice = availableDevices.first
            beginSession()
        }
    }

    func beginSession () {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
            captureSession.addInput(captureDeviceInput)
        } catch {
            print(error.localizedDescription)
        }

        if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
            self.previewLayer = previewLayer
            self.view.layer.addSublayer(self.previewLayer)
            self.previewLayer.frame = self.view.layer.frame
//ADD CONSTRAINTS HERE

            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.willkie.captureQueue)
//            dataOutput.setSampleBufferDelegate(self, queue: queue)
        }
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didDrop sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

why???

3 个答案:

答案 0 :(得分:6)

我认为可能会改变你的这一特定行 -

self.previewLayer.frame = self.view.layer.frame 

self.previewLayer.frame = self.view.layer.bounds

此外,只有在上面的代码中调整了帧后才应添加子图层。现在你已经添加了。

self.view.layer.addSublayer(self.previewLayer)

您还需要添加重力

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

如果它仍然不起作用,请将自定义视图添加到您的viewcontroller并将其设置为您要显示相机预览的大小,然后您可以使用它 -

self.previewLayer.frame = self.customViewOutlet.layer.bounds

答案 1 :(得分:1)

我用 Swift 4 这样解决了这个问题:

别忘了导入AVKit

override func viewDidLoad() {
    super.viewDidLoad()

    let captureSession = AVCaptureSession()

    guard let captureDevice = AVCaptureDevice.default(for: .video),
        let input = try? AVCaptureDeviceInput(device: captureDevice) else {return}

    captureSession.addInput(input)
    captureSession.startRunning()

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.frame
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)

}

答案 2 :(得分:0)

我不确定这是解决此问题的最佳方法,但对我来说有用的是将其设置为屏幕尺寸框架。因此,在这种情况下,它将是:

self.previewLayer.frame = UIScreen.main.bounds

希望这有帮助