简体   繁体   English

PhotoKit iOS8-使用“ PHImageFileURLKey”检索图像

[英]PhotoKit iOS8 - Retrieve image using the “PHImageFileURLKey”

Is there anyway I can use the Path returned from the "PHImageFileURLKey" to go into the photo library and retrieve the image? 无论如何,我可以使用从"PHImageFileURLKey"返回的路径进入照片库并检索图像吗?

The path returned is in this format: 返回的路径采用以下格式:

"file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG"

My plan is to store this path in the database and use it to fetch the image when I need to get it back. 我的计划是将此路径存储在数据库中,并在需要取回图像时使用它来获取图像。

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

I think your solution of retrieving Photo Kit asset from the URL is wrong. 我认为您从URL检索Photo Kit资产的解决方案是错误的。

Here is what I would do (supposing you have access to PHAsset): 这是我要做的(假设您可以访问PHAsset):

Store the localIdentifier: 存储localIdentifier:

PHAsset *asset = /*Your asset */
NSString *localIdentifier = asset.localIdentifier;

//Now save this local identifier, or an array of them

When it is time to retrieve them you simply do: 当需要检索它们时,您只需执行以下操作:

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:savedLocalIdentifiers options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
    //this gets called for every asset from its localIdentifier you saved
}];

If you only have access to “PHImageFileURLKey” then disregard this answer. 如果您只能访问“ PHImageFileURLKey”,请忽略此答案。

This isn't documented, so I'd strongly advise against using that URL for anything more than a prototype app. 这没有记录,所以我强烈建议您不要将该URL用于原型应用程序以外的任何用途。 That said, this does appear to work: 就是说,这看起来确实有效:

dispatch_queue_t queue = dispatch_queue_create("photoLoadQueue", 0);
dispatch_async(queue, ^{
    NSURL *privateUrl = [NSURL URLWithString:@"file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
    NSData *imageData = [NSData dataWithContentsOfURL:privateUrl];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = [UIImage imageWithData:imageData];
    });
});

Naturally you'll need to replace the string used to initiate the url with one which is valid for your phone. 自然,您需要用对手机有效的字符串替换用于启动url的字符串。

There are probably a load of issues with doing this - it's just not how the framework is meant to be used. 这样做可能会带来很多问题-并不是框架的使用方式。 Here are some off the top of my head: 这是我头上的一些东西:

  1. When running in the simulator, the root path changes regularly between launches of the app, so if you store absoluteUrls like this your database will quickly become full of dead URLs. 在模拟器中运行时,根路径在应用程序的启动之间会定期更改,因此,如果您存储诸如此类的absoluteUrl,则数据库将很快充满无效URL。 This will be inconvenient to say the least. 至少可以这样说。
  2. Worse, the URLs for the images may change on a real device - you don't have control over it, and once they change it's your app's fault for making the user reselect them or whatever. 更糟糕的是,图像的URL可能会在真实设备上更改-您无法对其进行控制,一旦更改,则是应用程序让用户重新选择图像或其他原因的错误。
  3. You're not going to ever find out about changes to the PHAsset which the photo came from. 您将永远不会找到有关照片来源的PHAsset更改。
  4. This may be circumventing user permission for photo access - what happens if your app's permission to access photos is revoked? 这可能会绕过用户的照片访问权限-如果您的应用程序的照片访问权限被撤销,会发生什么情况? This is probably an issue with lots of approaches to storing photos for later use, however. 但是,这可能是许多方法存储照片供以后使用的问题。
  5. You don't control the file - what if the user deletes it? 您不控制文件-如果用户将其删除怎么办?

If I were you, I would retrieve the image properly from the photos framework, using PHImageManager requestImageForAsset: targetSize: contentMode: options: resultHandler: , and store it in a file within your app's directory, at a sensible resolution for whatever you're doing with it. 如果您是我,可以使用PHImageManager requestImageForAsset: targetSize: contentMode: options: resultHandler:从照片框架正确检索图像,并将其存储在应用程序目录内的文件中,以适合您正在执行的操作的分辨率用它。 This still doesn't give you asset changes, but is a pretty good solution. 这仍然不会给您带来资产变化,但是是一个很好的解决方案。

If you want to store the assets themselves and only request the images when you actually need them, it might be worth investigating transient asset collections , though I've not used them so that might not work for what you need. 如果您想自己存储资产并仅在实际需要它们时才请求图像,那么也许值得研究一下瞬态资产集合 ,尽管我没有使用过它们,因此可能无法满足您的需求。

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

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