如何在没有ALAssetsLibrary的情况下从“UIImagePickerControllerReferenceURL”获取UIImage

时间:2016-05-05 03:35:17

标签: ios objective-c uiimage uiimagepickercontroller

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
    NSLog(@"url : %@",url);
}

我知道我们总是使用ALAssetsLibrary通过UIImagePickerControllerReferenceURL获取UIIMage。但它已被弃用,我还有其他方式吗?

顺便说一下,代码的输出如下:

assets-library://asset/asset.JPG?id=106E99A1-4F6A-45A2-B320-B0AD4A8E8473&ext=JPG
我检查了苹果官方文档,发现ALAssetsLibrary被PHPhotoLibrary取代。

我的天啊!最后我发现,这是答案:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
    PHAsset *asset = result.firstObject;
    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestImageForAsset:asset targetSize:CGSizeMake(100, 100) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        NSLog(@"%@",result);
    }];
}

1 个答案:

答案 0 :(得分:1)

因为在ios9中不推荐使用ALAssetsLibrary。使用Photos Framework Reference.