问题
I have image path and I want to display it in uiimageview,
/Users/WTS-New/Library/Developer/CoreSimulator/Devices/F43C26D2-DCFF-4764-AAF0-F6CC7BCDCF5D/data/Containers/Data/Application/8E49D340-7CAC-4031-85BF-9E5C26A1E37A/Documents/Small-mario.png
How can I get image to displayed?
回答1:
Try using below code:
Get image path from Image name:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFilePath;
NSError *err;
imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"Small-mario.png"];
Set image using imageWithContentsOfFile method:
YourImageView.image = [UIImage imageWithContentsOfFile: imageFilePath];
回答2:
To display image from complete file path on device
NSString *path = @"<your file path>;
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithData:data];
*The key is using fileURLWithPath instead of URLWithString when converting to NSURL
来源:https://stackoverflow.com/questions/39248712/display-image-from-path-in-imageview-in-ios