2

我正在尝试将 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的内容: 在此处输入图像描述

4

5 回答 5

2

plist不正确,应该是

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

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

NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];     
NSArray *imageArray = [picturesDictionary objectForKey:@"PhotosArray"]; 
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
于 2012-06-01T08:42:06.213 回答
1

尝试使用这个:

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

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

于 2012-06-01T08:13:32.427 回答
1
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];

这部分应该是这样的

PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
于 2012-06-01T08:14:12.027 回答
0

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

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

[UIImage imageNamed:[imageArray objectAtIndex:1]];
于 2012-06-01T08:15:54.037 回答
0

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

[controller.view addSubview:PhotosInAlbum];

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

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

于 2012-06-01T08:40:44.230 回答