For me, this is quite a mouthful of code. I understand the pieces of each code part but I cannot describe the logic flow of what how it hangs together and works as a whole, starting with the interpretation of the '^' character after the completionHandler:
method.
May I ask for some help here to re-write this code in a less compressed form that is less efficient but more visually understandable? I downloaded this code and I can state that in the context of the program, it is working code.
Thanks.
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer,
kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
NSLog(@"attachements: %@", exifAttachments);
}
else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput
jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self setStillImage:image];
[image release];
[[NSNotificationCenter defaultCenter]
postNotificationName:kImageCapturedSuccessfully object:nil];
}];
The ^
symbols the start of a block. Basically what it's doing is the code inside the block (from ^{
to }
) isn't called until the method captureStillImageAsynchronouslyFromConnection
is completed. Once the method has finished capturing the image, it then performs the methods inside the block. Using blocks is relatively new in the Mac & iPhone world, but they save you from lots of messy delegate methods and are generally awesome to use. At first they may seem daunting, but you'll learn to love them soon enough.
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