Khi phương thức đại biểu imagePickerController: didFinishPickingMediaWithInfo: đối với UIImagePickerController được gọi, bạn nhận được URL nội dung cho ảnh cụ thể được chọn.
[info valueForKey:UIImagePickerControllerReferenceURL]
Bây giờ URL này có thể được sử dụng để truy cập nội dung trong ALAssetsLibrary. Sau đó, bạn sẽ cần một ALAssetRepresentation của tài sản được truy cập đó. Từ ALAssetRepresentation này, chúng tôi có thể nhận được UTI cho hình ảnh đó (http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html)
Có lẽ mã sẽ làm cho nó một chút rõ ràng hơn:
- (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);
}];
}
}
Khi UTI giải thích, điều này nên được một shot câu trả lời chắc chắn như thế nào hình ảnh được lưu trữ trong thư viện ảnh.
Nguồn
2013-06-11 17:33:39
Đôi khi bạn cần phải quan tâm, bởi vì nếu bạn thực hiện UIImageJPEGRepresentation từ hình ảnh jpeg, bạn có thể nén không cần thiết ảnh đã được nén (mất chất lượng và thời gian xử lý). Ví dụ: bạn có thể cần hình ảnh được chọn để được xử lý thêm bởi thư viện chỉ chấp nhận jpeg. –