populating an array with images from a plist file
I'm trying to make an array of objects (animalImages) from a plist file which contains the name of the images to pull from my resources folder. I tried using a for loop to add each image individually but got开发者_StackOverflow lost in the logic somehow. This is what I have:
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]]]];
}
I imagine I'm probably going to be smacking my head once I get an answer to this but I am just confused about the order of operations in the initWithImage section of this code.
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 < [animalImageNames count]; i++) {
NSString *name = [animalImageNames objectAtIndex:i];
[animalImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", name]]];
}
There might be some minor erros, as I have not compiled the code.
精彩评论