0

How can I get the path or directory of the PHAsset images?

I came across an answer here written by @diegomen but when I try it like selectedAssets[0].PHAsset.getURL() it asks for a parameter but @diegomen said it doesn't require a parameter from the comments.

Am I applying it wrong?

Or is there other way of getting the images path from PHAsset?

NOTE: I found another solution here by @Clay but it doesn't seem to be the good practice to do it.

Source
  • 99
  • 1
  • 1
  • 7
  • Yes, you need to pass a completion block to that answer's `getURL` method. Read all of the comment below that answer. – rmaddy Nov 02 '17 at 20:45
  • PHAsset is not image. It is structure wich contain many information about image and image data. You can get UIImage from this with some quality. – Pavlo Boiko Nov 02 '17 at 21:47

2 Answers2

2

If you have already store image in your local file path and you want to retrive the image file Path from your PHAsset image address then use this function:

[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
    NSURL *imageURL = contentEditingInput.fullSizeImageURL;
    }];

asset=PHAsset *asset

khusboo suhasini
  • 293
  • 2
  • 16
-1

You should store image to local file. After that you will have file url.

NSData *imageData = UIImagePNGRepresentation(newImage);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]];
[imageData writeToFile:imagePath atomically:NO];
Pavlo Boiko
  • 583
  • 5
  • 13