Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AVCaptureStillImageOutput ::captureStillImageAsynchronouslyFromConnection:completionHandler is never called?

I try to build an app which captures frames from iPhone camera and does some processing with these frames. I tried some pieces of codes found on the Internet, e.g. here: How to capture image without displaying preview in iOS

and I ended up with the following piece of code:

-(IBAction)captureNow{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    //  NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             NSLog(@"attachements: %@", exifAttachments);
         }
         else
             NSLog(@"no attachments");

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         //  do the processing with the image       
         }

However, the app never gets into the handler code block of the captureStillImageAsynchronouslyFromConnection method so I never get the image from the frame. Am I doing something in a wrong way? Thanks for your suggestions!

like image 787
Jakub Avatar asked Feb 04 '13 15:02

Jakub


2 Answers

I've discovered that the pointer to AVCaptureSession doesn't get retained properly by AVCaptureStillImageOutput in Automatic-Reference-Counting mode.

This means you have two options:

  • disable automatic reference counting, or

  • retain the pointer to AVCaptureSession yourself (for example, by assigning it to a strong property in your controller).

like image 92
thatha Avatar answered Nov 04 '22 07:11

thatha


You might want to check the NSError *error parameter. It gives you information about the picture being successfully taken. AVFoundation error codes are here. I hope this helps.

Also you might want to read about this, where he explains having session code at same place could lead to problems.

like image 36
thar_bun Avatar answered Nov 04 '22 05:11

thar_bun