EcksMedia
EcksMedia

Reputation: 461

Iphone - Load images to array using a loop

OK, I am sure that there is a simple way to do this but I just can't find the answer anywhere.

I have a whole stack of images that I need to load for several different UIImageView animations. I want these to load using a loop because there are so many but I can't work out how to isolate the correct images in the bundle.

So for example, say I have 40 image files. The first 13 are named jack_1.png up to jack_13.png the next 16 are called jill_1.png up to jill_16.png and 11 called hill_1.png up to hill_11.png.

I would like to create an for/if statement that loads all @"jack_%i.png" files into on array, all @"jill_%i.png" files into another Array and so on.

I hope this make sense.

Upvotes: 0

Views: 479

Answers (2)

Jesse Naugher
Jesse Naugher

Reputation: 9810

Use imageNamed: and stringWithFormat:.. something like:

for(int i=1;i<=13;i++)
{
 [jackArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"jack_%d.png", i]]];
}

I would like to add that spam initializing images like this generally isn't a great performance idea.

Upvotes: 1

arclight
arclight

Reputation: 5310

This is the code for the jack Array, the code for the others would be the same but changing the relevant pieces (the limit in the loop and the format for the name)

for (int i = 1; i <= 13; i++) {
    [jackArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"jack_%d.png", i]]];
}

Upvotes: 2

Related Questions