2011-01-06 58 views
0

我試圖從一個plist文件中製作一個包含要從我的資源文件夾中拉出的圖像名稱的對象數組(animalImages)。我嘗試使用for循環來單獨添加每個圖像,但以某種方式迷失在邏輯中。這是我有:從plist文件中填充圖像

NSString *images = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]; //string with resource path    

animalImageNames = [[NSMutableArray alloc] initWithContentsOfFile:images]; //array with file names 

int i; 
int j = 10; 

for (i=0; i <=j; i++) { 
animalImages = [[NSArray alloc] initWithObjects:   
       [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"%@.png",[animalImageNames objectAtIndex:i]]]]; 
} 

我想我可能會被拍打我的頭,一旦我得到一個答案,但我只是困惑業務的這段代碼的initWithImage部分的順序。

回答

4

NSString *images = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]; 

// need the names for just now. creating an autoreleased one 
// and no need to be mutable 
NSArray *animalImageNames = [NSArray arrayWithContentsOfFile:images]; 

// this array will contain the images 
// need to release this when you are done 
NSMutableArray *animalImages = [[NSMutableArray alloc] init]; 

// loop through all names from the plist and add image to the array 
for (NSInteger i = 0; i &lt; [animalImageNames count]; i++) { 
    NSString *name = [animalImageNames objectAtIndex:i]; 
    [animalImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", name]]]; 
} 

可能有一些輕微的錯誤回報,因爲我還沒有編譯的代碼。

+0

謝謝!是的,這就像一個魅力 – user550323 2011-01-06 22:16:32