当调用 UIImagePickerController 的委托方法 imagePickerController:didFinishPickingMediaWithInfo: 时,您将获得所选照片的资产 URL。
[info valueForKey:UIImagePickerControllerReferenceURL]
现在,此 URL 可用于访问 ALAssetsLibrary 中的资产。然后,您将需要该访问资产的 ALAssetRepresentation。从这个 ALAssetRepresentation 我们可以得到该图像的 UTI ( http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html )
也许代码会使它更清晰一些:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (!(picker.sourceType == UIImagePickerControllerSourceTypeCamera)) {
NSLog(@"User picked image from photo library");
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *repr = [asset defaultRepresentation];
if ([[repr UTI] isEqualToString:@"public.png"]) {
NSLog(@"This image is a PNG image in Photo Library");
} else if ([[repr UTI] isEqualToString:@"public.jpeg"]) {
NSLog(@"This image is a JPEG image in Photo Library");
}
} failureBlock:^(NSError *error) {
NSLog(@"Error getting asset! %@", error);
}];
}
}
正如 UTI 解释的那样,这应该是图像如何存储在照片库中的可靠答案。