I'm trying to store my images in an array named "_images" but if I use NSLog()
to view the data stored in image array, I get only one image. Would you guys help me out? Here's my code:
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo1.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo2.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo3.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo4.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo5.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo6.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo7.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo8.png"]];
_images =[NSMut开发者_开发知识库ableArray arrayWithObject:[UIImage imageNamed:@"logo9.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo10.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo11.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo12.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo13.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo14.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo15.png"]];
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:@"logo16.png"]];
NSLog(@"ha ha ha:%d",_images.count);
You're creating a new array on each line; you lose the reference to the old array and thus the image within it. You want to add new images to an existing array. Change lines 2 and onwards to the following:
[_images addObject:[UIImage imageNamed:...]];
You can also use a loop:
_images = [[[NSMutableArray alloc] init] autorelease];
for (int i=1; i<=16; ++i) {
[_images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"logo%d.png",i]]];
}
精彩评论