xcode 在ios中的imageView中显示路径中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39248712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:11:26  来源:igfitidea点击:

Display image from path in imageView in ios

iosobjective-cxcodeuiimageviewuiimage

提问by KAR

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

我有图像路径,我想在 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

/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?

我怎样才能显示图像?

回答by Ronak Chaniyara

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 imageWithContentsOfFilemethod:

设置图像使用imageWithContentsOfFile方法:

YourImageView.image = [UIImage imageWithContentsOfFile: imageFilePath]; 

回答by Alfred

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 fileURLWithPathinstead of URLWithStringwhen converting to NSURL

*密钥正在使用fileURLWithPath而不是URLWithString转换为NSURL