0

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.

2 Answers 2

2

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]]];
}
Sign up to request clarification or add additional context in comments.

2 Comments

yours doesn't add jack_13.png :)
Thanks arclight. I was on the same track as you with this code but I am hoping that there may be a way to do this so that I don't need to know how many images are in each group. I have been trying to scan the file names but had no success? Your solution will get me out of trouble but it would be nice if this process were more automated.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.