[英]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: 这是我头上的一些东西:
PHAsset
which the photo came from. 您将永远不会找到有关照片来源的PHAsset
更改。 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.