2012-06-01 85 views
2

我想從plist文件到的UIImageView顯示各種圖像加載圖像,我寫我的代碼是這樣的:的iOS:從plist文件

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

但實際上什麼也沒有發生,我的ImageView的是空的!我也有兩個按鈕來轉發後面的圖像。

plist中的內容: enter image description here

+0

你能顯示.plist的內容嗎? – samfisher

+0

@samfisher檢查我編輯的問題 –

+0

'[PhotosInAlbum發佈];'..但是'alloc'在哪裏? – Kjuly

回答

2

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]]; 
+0

謝謝你完成....! –

1

嘗試使用這樣的:

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

[imageArray objectAtIndex:1]只會返回形象的名字而不是它的路徑,所以爲了使用imageWithContentsOfFile你必須指定的路徑你圖像不僅僅是圖像名稱。

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

這部分應該是這樣的

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

同樣的結果!我沒有顯示在UIImageView –

+0

@Mc。情人你分配PhotosInAlbum? – Umgre

0

imageWithContentsOfFile:使用是該文件的全部或部分路徑的路徑。 您可以使用imageNamed:方法(使用文件的名稱)。只需更換

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

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

同樣的結果!我在UIImageView中沒有顯示 –

+1

檢查您是否將這些圖像包含在您的項目中? –

+0

你會看到我編輯的問題嗎? –

0

最初,[UIImage imageNamed:[imageArray objectAtIndex:1]];會解決烏爾問題。 但你不能這樣,它看起來像 的問題似乎是與此行

[controller.view addSubview:PhotosInAlbum]; 

它可能是這樣的控制器解決這個問題。 查看目前爲零,您正試圖添加您的imageview。

只是確保它不是。如果是cos,那麼您的PhotosInAlbum根本不會被添加爲controller.view的子視圖。