从从PHImageFileURLKey获得的URL中读取

时间:2014-10-10 01:16:49

标签: ios image file ios8

适用于iOS 8的Photo Framework能够使用PHImageFileURLKey返回图像的URL。但是,我似乎无法阅读此URL。我只需要读访问权限。到目前为止,我试图使用给定的URL的任何地方,它被视为文件根本不存在。如何使用URL获取数据?

成功返回PHAssets和网址的代码:

PHFetchResult* phFetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]
PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions* options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES; // Force sequential work. We have nothing to do until this block returns.
PHAsset* asset = [phFetchResult objectAtIndex:index];
[manager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage *resultImage, NSDictionary *info)
 {
     _image = resultImage;
     NSURL* fileURL = [info objectForKey:@"PHImageFileURLKey"];
     _imageLocation = [fileURL relativePath];
 }];

代码显示未找到任何内容:

NSURL* fileURL = [info objectForKey:@"PHImageFileURLKey"];
NSLog(@"File exist?: %d", [[NSFileManager defaultManager] fileExistsAtPath:[fileURL relativePath]]);

我目前的解决方法是获取给定的UIImage并将其保存到/ tmp目录,然后使用该URL。这不是一个理想的解决方案,我真的很想知道PHImageFileURLKey的目的

1 个答案:

答案 0 :(得分:1)

我研究PHImageFileURLKey,我认为无法通过PHImageFileURLKey查找图像数据,但您可以使用资产本地标识符而不是PHImageFileURLKey来查找图像数据。

我的代码供您参考:

您可以使用资产的本地标识符,并将图片作为回应。

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:@[asset.localIdentifier] options:nil];
    [savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {

        //this gets called for every asset from its localIdentifier you saved
        PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
        imageRequestOptions.synchronous = YES;
        imageRequestOptions.deliveryMode = PHImageRequestOptionsResizeModeFast;

        [[PHImageManager defaultManager]requestImageForAsset:asset targetSize:CGSizeMake(50,50) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            NSLog(@"get image from result");

        }];
    }];