0

I am having some problems here with delegate. I have searched in Google and could not find the answer that help me. So I just wrote AVCapturePhotoCaptureDelegate because someone told me. what should I do ?(what to do with delegate)

stillImageOutput?.capturePhoto(with: sampleBuffer!, delegate: AVCapturePhotoCaptureDelegate){
            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
            }                
        }
Gordon Lee
  • 88
  • 1
  • 7
  • What exactly you want to achieve by stillImageOutput ? Are you using a capture session ? As I am unable to find this function in AVCaptureStillImageOutput() – Manish Pathak Dec 29 '16 at 14:13
  • yes, I am using capture session.I could not find(Apple removed it and removed stillImageOutput) AVCaptureStillImageOutput() , that's why I used AVCapturePhotoOutput() and capturePhoto(with:, delegate:) – Rob Manasaryan Dec 29 '16 at 14:18
  • It is available for the latest iOS version, it will be deprecated in the future version. So you are unable to get image from capture session using AVCapturePhotoOutput() correct ? – Manish Pathak Dec 29 '16 at 14:20
  • I was trying to capture the photo – Rob Manasaryan Dec 29 '16 at 14:21
  • yes , you got it right – Rob Manasaryan Dec 29 '16 at 14:22
  • http://stackoverflow.com/questions/37869963/how-to-use-avcapturephotooutput / Have you tried this link ? – Manish Pathak Dec 29 '16 at 14:25
  • self.stillImageOutput.capturePhoto(with: settings, delegate: self) Here you have to pass the settings and it will invoke the delegate didFinishProcessingPhotoSampleBuffer I am working on it as soon as I get it working I will share the answer with you. – Manish Pathak Dec 29 '16 at 14:30
  • I am trying .I will appreciate,if you will share the answer with me :) – Rob Manasaryan Dec 29 '16 at 14:39
  • 1
    `delegate: AVCapturePhotoCaptureDelegate` why is written in Capital?! I mean that's a *class* not an *instance*. The delegate should always be an instance (of a class) never a class – mfaani Dec 29 '16 at 15:39
  • @ManishPathak the function documentation: https://developer.apple.com/reference/avfoundation/avcapturephotooutput/1648765-capturephoto – Cœur Dec 29 '16 at 17:30

1 Answers1

0

AVCapturePhotoCaptureDelegate is a protocol. You can read up more on protocols here https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html but the relevant information is this:

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

What this function capturePhoto is asking you to do is to pass an Object that conforms to the protocol. By convention we call this a delegate (think of it as a series of functionality that's delegated to another object).

It's essentially a call back. capturePhoto "knows" (because you are conforming to the protocol) that the delegate will contain the functions that it needs to call so that you can do your processing of the image.

To successfully pass the delegate, you need to define an object which conforms to the protocol and pass an instance of that object to the function as the delegate. You MUST declare the methods that you intend to use from the protocol. Here's a partial EXAMPLE - don't copy paste this, you'll need to work out what you need for your implementation.

class PhotoDelegate() : AVCapturePhotoCaptureDelegate {
    optional func capture(_ captureOutput: AVCapturePhotoOutput, 
    didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, 
    previewPhotoSampleBuffer: CMSampleBuffer?, 
    resolvedSettings: AVCaptureResolvedPhotoSettings, 
    bracketSettings: AVCaptureBracketedStillImageSettings?, 
    error: Error?) {
        // YOUR CODE TO PROCESS THE IMAGE GOES HERE
    }
}

let myPhotoDelegate = PhotoDelegate()
// myPhotoDelegate is what you'll pass to the delegate: parameter

I would very carefully note that according to the documentation for AVCapturePhotoCaptureDelegate the methods are all OPTIONAL at compile time, which actual methods need to be implemented will depend on what settings you pass.

Which delegate methods the photo output calls depends on the photo settings you initiate capture with. All methods in this protocol are optional at compile time, but at run time your delegate object must respond to certain methods depending on your photo settings:

Tim Bull
  • 2,375
  • 21
  • 25