问题
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