正如 holtmann 提到的,读取 UIImagePickerControllerReferenceURL 会触发位置服务提示。如果您只需要图像数据,而不需要元数据,则可以从 UIImagePickerControllerOriginalImage 和 UIImagePickerControllerEditedImage 键获取它(我先检查 EditedImage,然后检查 OriginalImage 如果 EditedImage 为空)。这不需要使用资产库,也不需要位置访问。
这是我在我的应用程序中使用它的方式,包括保存图像的本地副本以供进一步编辑:
- (void)imagePickerController:(UIImagePickerController *)controller didFinishPickingMediaWithInfo:(NSDictionary *)info {
// get the selected photo as a UIImage
UIImage *photo = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if (!photo) {
photo = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
// save the photo to the app's Documents folder
if (photo) {
NSString *extension = @"jpg";
NSString *filename = [NSString stringWithFormat:@"%@.%@", self.defaultTitle, extension]; // self.defaultTitle is defined elsewhere in my app
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
[UIImageJPEGRepresentation(photo, 0.8) writeToFile:path atomically:YES];
}
}