You can use ALAssetsLibrary and ALAssetRepresentation to get the source data. Example:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL]; ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library assetForURL:imageURL resultBlock:^(ALAsset *asset) { ALAssetRepresentation *repr = [asset defaultRepresentation]; NSUInteger size = repr.size; NSMutableData *data = [NSMutableData dataWithLength:size]; NSError *error; [repr getBytes:data.mutableBytes fromOffset:0 length:size error:&error]; } failureBlock:^(NSError *error) { }]; }
But there are some things to consider:
assetForURL: works asynchronously.- On the device, using
assetForURL: will result in a confirmation dialog, which can annoy the user:
"Your application" would like to use your current location. This allows you to access location information in photos and videos.
- If the user denies access,
assetForURL: causes a block of failures. - The next time you use this method,
assetForURL: will fail without asking the user again. Only if you specified reset location alerts in the system settings will the user again ask.
So, you should be prepared for the failure of this method and use UIImageJPEGRepresentation or UIImagePNGRepresentation as a backup. But in this case, you will not get the source data, for example. metadata (EXIF, etc.) are missing.
source share