问题
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.
回答1:
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
回答2:
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];
来源:https://stackoverflow.com/questions/47083712/how-to-get-the-path-of-an-image