将图像存储在数组中

时间:2011-06-03 06:11:07

标签: iphone objective-c ios xcode

我正在尝试将我的图像存储在名为“_ images”的数组中,但如果我使用NSLog()来查看存储在图像数组中的数据,我只会得到一个图像。你能帮助我吗?这是我的代码:

_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 =[NSMutableArray 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);

2 个答案:

答案 0 :(得分:9)

你在每一行创建一个新数组;你失去了对旧数组的引用,从而失去了对它内部图像的引用。您想要将新图像添加到现有阵列。将第2行及以后更改为以下内容:

[_images addObject:[UIImage imageNamed:...]];

答案 1 :(得分:8)

您还可以使用循环:

_images = [[[NSMutableArray alloc] init] autorelease];
for (int i=1; i<=16; ++i) {
  [_images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"logo%d.png",i]]];
}