in Swift 2.3 I used this code to take a picture in custom camera:
func didPressTakePhoto(){
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) {
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProviderCreateWithCFData(imageData)
let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
self.captureImageView.image = image
}
})
}
}
But his line: stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
Shows this error:
Value of type 'AVCapturePhotoOutput' has no member 'captureStillImageAsynchronouslyFromConnection'
I tried solving my problem but I always get more and more errors so that is why I post my original code.
Does anybody know how to make my code work again?
Thank you.
You can use
AVCapturePhotoOutput
like this in Swift 3:You need the
AVCapturePhotoCaptureDelegate
which returns theCMSampleBuffer
.You can get as well a preview image if you tell the
AVCapturePhotoSettings
the previewFormatThanks to Sharpkits I found my solution (This code works for me):
Great code. Thanks a ton for the help and examples.
Just to clarify for those of slower mental faculties like myself, the capture(_ ...etc) method is called behind the scenes when you call self.cameraOutput.capturePhoto(with: settings, delegate: self) inside of your takePhoto (or whatever you name it) method. You will never actually call the capture method yourself directly. It happens automatically.