Swift 3 - Capturing a photo on custom camera view

2019-08-01 21:45发布

I am using Swift 3, Xcode 8.2.

A lot of the tutorials that I find on this topic seem to always be in Swift 2. I have a custom camera view already created and I am trying to capture a photo from it.

I've set up an @IBAction function below. The variable session_output is AVCapturePhotoOutput:

@IBAction func shutterButtonPressed() {
    session_output.capturePhoto(with: AVCapturePhotoSettings.init(format: [AVVideoCodecKey : AVVideoCodecJPEG]), delegate: <#T##AVCapturePhotoCaptureDelegate#>)
}

I don't know what to put in the delegate field and how to read the photo from the buffer after it is captured. The difference between Swift 2 and 3 is so stark in this case that I can't even bumble my way through it which I've been fairly successful at doing when following most Swift 2 tutorials.

Any help would be greatly appreciated.

1条回答
时光不老,我们不散
2楼-- · 2019-08-01 22:05

set delegate to self and use this delegate AVCapturePhotoCaptureDelegate

And you can get captured image from below delegate

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: NSError?) {

    if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
      print(image: UIImage(data: dataImage).size)
    }

}
查看更多
登录 后发表回答