I'm trying to read in EXIF data from images in the iOS camera roll using the fantastic code here:
http://blog.codecropper.com/2011/05/getting-metadata-from-images-on-ios/
Unfortunately, on the first attempt to read the data, nil is returned ... but every subsequent attempt works fine.
The author of the blog is aware of this and states a solution, but I just don't understand it at all! I'm new to "blocks" and I'm just not getting it even though I've read: http://thirdcog.eu/pwcblocks/
Can anyone translate for me?
This is the code being used to read in the data:
NSMutableDictionary *imageMetadata = nil;
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
NSDictionary *metadata = asset.defaultRepresentation.metadata;
imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:metadata];
[self addEntriesFromDictionary:metadata];
}
failureBlock:^(NSError *error) {
}];
[library autorelease];
which is conveniently put into an init method and called like:
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] initWithInfoFromImagePicker:info];
The authors description of the first attempt problem is:
One caveat on using this: because it uses blocks, there’s no guarantee that your imageMetadata dictionary will be populated when this code runs. In some testing I’ve done it sometimes runs the code inside the block even before the [library autorelease] is executed. But the first time you run this, the code inside the block will only run on another cycle of the apps main loop. So, if you need to use this info right away, it’s better to schedule a method on the run queue for later with:
[self performSelectorOnMainThread:SELECTOR withObject:SOME_OBJECT waitUntilDone:NO];
.. and it's this line I'm stuck at! I don't know what to do with it?
Any help greatly appreciated!