[英]iOS : Load image from plist file
我试图将plist文件中的各种图像显示到UIImageView中,我这样编写代码:
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];
imageArray = [picturesDictionary objectForKey:@"PhotosArray"];
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
但是实际上什么也没发生,而我的ImageView是空的! 我还有两个按钮可以向后转发图片。
Plist的内容:
plist不正确,应该是
Root ---- Dictionary
PhotosArray ----- Array
Item 0 ------ String -- Beach.jpg
然后将此代码添加到您的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]];
尝试使用此:
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
您的[imageArray objectAtIndex:1]
将仅返回图像名称,而不是其路径,因此,要使用imageWithContentsOfFile,您必须指定图像的路径,而不仅是图像名称。
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
这部分应该是这样的
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
imageWithContentsOfFile:
使用路径作为文件的完整或部分路径。 您可以改用imageNamed:
方法(使用文件名)。 只需更换
[UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
至
[UIImage imageNamed:[imageArray objectAtIndex:1]];
最初, [UIImage imageNamed:[imageArray objectAtIndex:1]];
本来可以解决您的问题。 但是您无法通过这种方式解决此问题,看起来问题似乎出在此行上
[controller.view addSubview:PhotosInAlbum];
您的控制器可能就是这种情况。 您尝试添加您的imageview时, view为nil。
只要确定不是。 如果是cos,那么您的PhotosInAlbum根本不会作为子视图添加到controller.view
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.