This question already has answers here:
Get NSData from assets-library URL

(2个答案)


4年前关闭。



-(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);
    }];
}

最佳答案

ios9中已弃用ALAssetsLibrary。使用Photos Framework Reference.

关于ios - 我如何在没有ALAssetsLibrary的情况下从“UIImagePickerControllerReferenceURL”获取UIImage ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37041881/

10-09 02:06