简体   繁体   中英

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?

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.

Here is what I would do (supposing you have access to PHAsset):

Store the 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.

This isn't documented, so I'd strongly advise against using that URL for anything more than a prototype app. 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.

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. 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.
  3. You're not going to ever find out about changes to the PHAsset which the photo came from.
  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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