Display image from path in imageView in ios [duplicate]

我是研究僧i 提交于 2019-12-25 07:47:15

问题


I have image path and I want to display it in uiimageview,

/Users/WTS-New/Library/Developer/CoreSimulator/Devices/F43C‌​26D2-DCFF-4764-AAF0-‌​F6CC7BCDCF5D/data/Co‌​ntainers/Data/Applic‌​ation/8E49D340-7CAC-‌​4031-85BF-9E5C26A1E3‌​7A/Documents/Small-m‌​ario.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-m‌​ario.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

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