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!
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With