xcode iOS:从 plist 文件加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10846620/
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 00:27:48  来源:igfitidea点击:

iOS : Load image from plist file

iphoneobjective-ciosxcodeipad

提问by Mc.Lover

I am trying to show various images from a plist file into UIImageView , I wrote my code like this :

我试图将 plist 文件中的各种图像显示到 UIImageView 中,我这样编写代码:

NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];    
    imageArray = [picturesDictionary objectForKey:@"PhotosArray"];
    PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];

But actually nothing happens, and my ImageView is empty ! also I have two buttons to forward backward images .

但实际上什么也没发生,我的 ImageView 是空的!我也有两个按钮来向前向后图像。

Contents of Plist : enter image description here

Plist 的内容: 在此处输入图片说明

采纳答案by DLende

The plist is incorrect, it should be

plist不正确,应该是

Root ---- Dictionary
     PhotosArray ----- Array
         Item 0   ------ String -- Beach.jpg

Then add this code to your viewController.. Be sure that the Interface imageView is link to IBOutlet.

然后将此代码添加到您的 viewController.. 确保接口 imageView 链接到 IBOutlet。

NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];     
NSArray *imageArray = [picturesDictionary objectForKey:@"PhotosArray"]; 
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];

回答by Nuzhat Zari

Try using this:

尝试使用这个:

 PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];

Your [imageArray objectAtIndex:1]will only return image name not its path, so in order to use imageWithContentsOfFile you have to specify the path of your image not only image name.

[imageArray objectAtIndex:1]将只返回图像名称而不是其路径,因此为了使用 imageWithContentsOfFile,您必须指定图像的路径而不仅仅是图像名称。

回答by Umgre

PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];

this part should be like this

这部分应该是这样的

PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];

回答by Kjuly

imageWithContentsOfFile:use a path that is the full or partial path to the file. You can use imageNamed:method instead (which use the name of the file). Just replace

imageWithContentsOfFile:使用作为文件的完整或部分路径的路径。您可以改用imageNamed:方法(使用文件名)。只需更换

[UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];

to

[UIImage imageNamed:[imageArray objectAtIndex:1]];

回答by Saurabh Passolia

initially, [UIImage imageNamed:[imageArray objectAtIndex:1]];would have solved ur problem. But you are not able to resolve this issue this way which looks like the problem seems to be with this line

最初,[UIImage imageNamed:[imageArray objectAtIndex:1]];会解决你的问题。但是您无法以这种方式解决此问题,看起来问题似乎与此行有关

[controller.view addSubview:PhotosInAlbum];

it might be the case your controller.viewis nil at the moment you are trying to add ur imageview.

您的控制器可能就是这种情况。在您尝试添加您的图像视图时,视图为零。

just make sure it isn't. cos if it is then your PhotosInAlbum is not at all getting added as subview to the controller.view.

只要确保它不是。cos 如果是,那么您的 PhotosInAlbum 根本不会作为子视图添加到controller.view.