I’m using the following code to load many images into an array, but want to know how to do it more efficiently. For example, how could I efficiently load 100 images into an array?
hopAnimation=[[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"frame-1.png"],
[UIImage imageNamed:@"frame-2.png"],
[UIImage imageNamed:@"frame-3.png"],
[UIImage imageNamed:@"frame-4.png"],
[UIImage imageNamed:@"frame-5.png"],
[UIImage imageNamed:@"frame-6.png"],
[UIImage imageNamed:@"frame-7.png"],
[UIImage imageNamed:@"frame-8.png"],
[UIImage imageNamed:@"frame-9.png"],
[UIImage imageNamed:@"frame-10.png"],
.
.
.
[UIImage imageNamed:@"frame-100.png"],
nil
];
Is there a way to use a for-loop to do this efficiently?
You can use a for loop, sure. You might want to put all the image names into a plist. Then you can create the array of names:
and loop through it to create the array of images:
Or, if your image files are really named consistently like that, you can loop on an integer and put that into the file name string: