Reputation: 3129
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!
Upvotes: 7
Views: 3636
Reputation: 307
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:
retain the pointer to AVCaptureSession
yourself (for example, by assigning it to a strong property in your controller).
Upvotes: 8
Reputation: 820
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.
Upvotes: 2