I've been figuring this out for a couple of days, and finally got a solution. As already suggested, you can use ALAssetsLibrary.
The image picker will give you a dictionary, which contains an url that points to the asset.
The ALAssetsLibrary's assetForURL: resultBlock: failureBlock:
method uses blocks. You can read more about them from example here.
So this is how we handle the info given by the picker:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary) {
// We'll store the info to use in another function later
self.imageInfo = info;
// Get the asset url
NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
// We need to use blocks. This block will handle the ALAsset that's returned:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
// Get the location property from the asset
CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
// I found that the easiest way is to send the location to another method
[self handleImageLocation:location];
};
// This block will handle errors:
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
// Do something to handle the error
};
// Use the url to get the asset from ALAssetsLibrary,
// the blocks that we just created will handle results
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
And next the method that handles the image and location data:
- handleImageLocation:(CLLocation *)location
{
UIImage *image = [self.imageInfo objectForKey:UIImagePickerControllerOriginalImage];
// Do something with the image and location data...
}
And of course you can also get other information about the image with this method by using keys:
ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate
ALAssetPropertyRepresentations
ALAssetPropertyURLs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…