How to get the path of an image?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 05:00:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!