khunshan
khunshan

Reputation: 2862

iOS10: What is AVCapturePhotoOutput equivalent for captureStillImageAsynchronouslyFromConnection?

AVCapturePhotoOutput is the library for deprecated AVCaptureStillImageOutput which allows to capture still ImageAsynchronously from video connection in iOS 10.

Upvotes: 5

Views: 3911

Answers (1)

OOPer
OOPer

Reputation: 47876

It's not a simple replacement of AVCaptureStillImageOutput.

Check the reference of AVCapturePhotoOutput:

Especially, you may need to read this part in Overview:

  1. Capture an image by passing your photo settings object to the capturePhoto(with:delegate:) method along with a delegate object implementing the AVCapturePhotoCaptureDelegate protocol. The photo capture output then calls your delegate to notify you of significant events during the capture process.

Actual code example is found in Apple's sample code: AVCam-iOS

See CameraViewController.swift

Digestively, you need two things, an instance of AVCapturePhotoSettings and an instance conforming to AVCapturePhotoCaptureDelegate. And then call capturePhoto(with:delegate:) method of AVCapturePhotoOutput.

let photoSettings = AVCapturePhotoSettings()
//setup `photoSettings`
//...
//create an instance conforming to `AVCapturePhotoCaptureDelegate`
let photoCaptureDelegate = PhotoCaptureDelegate(with: photoSettings,...)
//`AVCapturePhotoCaptureDelegate` can be a complex object, so in the sample code, implemented in a separate file as `PhotoCaptureDelegate`.
//You need to keep strong reference to the delegate instance while capturing in progress.
self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureDelegate)

The sample code would have some extraneous features that you may not need. You can simplify it with removing them.

(You are not specifying the language tag, but the sample code above includes Objective-C version.)

With this method of delegate you can fetch the image, similar methods also exist for RAW or Live Photo.

func capture(AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?)

Upvotes: 5

Related Questions