简体   繁体   English

从从PHImageFileURLKey获得的URL中读取

[英]Read from URL obtained from PHImageFileURLKey

The Photo Framework for iOS 8 is able to return a URL for images using the PHImageFileURLKey. 适用于iOS 8的Photo Framework能够使用PHImageFileURLKey返回图像的URL。 However I can't seem to read this URL. 但是,我似乎无法阅读此URL。 I only need read access. 我只需要读访问权限。 So far, any place I attempt to use the URL given, it is treated as though the file doesn't exist at all. 到目前为止,我试图使用给定的URL的任何地方,它被视为文件根本不存在。 How can I get data using the URL? 如何使用URL获取数据?

Code that successfully returns PHAssets and a URL: 成功返回PHAssets和URL的代码:

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

Code showing nothing found: 代码显示没有找到:

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

My current workaround is to take the UIImage given and save it to the /tmp directory and then use that URL. 我目前的解决方法是获取给定的UIImage并将其保存到/ tmp目录,然后使用该URL。 It's not an ideal solution and I'd really like to know the purpose of the PHImageFileURLKey 这不是一个理想的解决方案,我真的很想知道PHImageFileURLKey的目的

I research on PHImageFileURLKey and i think there is no way to find image data by PHImageFileURLKey but you can find your image data by using asset local identifier instead of PHImageFileURLKey . 我研究PHImageFileURLKey ,我认为没有办法通过PHImageFileURLKey查找图像数据,但您可以使用资产本地标识符而不是PHImageFileURLKey来查找图像数据。

My code for your reference: 我的代码供您参考:

You can use your asset's local identifier and will get the image in response. 您可以使用资产的本地标识符,并将图像作为响应。

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");

        }];
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM