Swift 3 - Capturing a photo on custom camera view

扶醉桌前 提交于 2019-12-13 00:37:42

问题


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:


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

}


来源:https://stackoverflow.com/questions/42546695/swift-3-capturing-a-photo-on-custom-camera-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!