xcode 将图像存储在数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6223603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:11:32  来源:igfitidea点击:

storing images in array

iphoneobjective-ciosxcode

提问by kingston

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”的数组中,但如果我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);

回答by Chris Doble

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:

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

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

回答by PengOne

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]]];
}