4

I am making a custom camera view controller using AVFoundation frameworks in Swift 4/iOS 11/Xcode 9.1

My code for setUpCaptureSession: (credit to brian voong of letsbuildthatapp.com, I have followed his IG/firebase tutorial and made extensive use of his work!)

fileprivate func setupCaptureSession() {
    let captureSession = AVCaptureSession()

    //setup inputs
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice!)
        if captureSession.canAddInput(input) {
            captureSession.addInput(input)
        }
    } catch let err {
        debugPrint(err)
    }
    //setup outputs

    if captureSession.canAddOutput(output) {
        captureSession.addOutput(output)
    }
    //setup output preview
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = photoPreView.frame
    view.layer.addSublayer(previewLayer)

    captureSession.startRunning()

}

photoPreView is a view I manually added to the storyboard with aspect ratio 1:1. my issue is visible in this screenshot below: viewFinder

The capture session is indeed within photoPreView.frame but I want the capture session to fill up the entire area and zoom in , much like how the square mode of the native iOS camera works, or how Instagram's photo capture appears as well.

Peracek
  • 1,801
  • 1
  • 15
  • 17
kinghenry14
  • 1,187
  • 1
  • 11
  • 34

1 Answers1

4

On your capture layer you should set...

previewLayer.videoGravity = .resizeAspectFill
Fogmeister
  • 76,236
  • 42
  • 207
  • 306