Build Array of Images with NSMutableArray


/ Published in: Objective C
Save to your folder(s)

Rather than creating an array of images where you list each image individually this will loop through images that all have the filename "frame-X.png" where X is a number from 1 to 20. So if you have a series of images "frame-1.png", "frame-2.png" this loop will create the array. Good for creating an image-based animation.


Copy this code and paste it in your HTML
  1. //First, define how many images will be in your array
  2. //Just change to adjust to your own number of images
  3. #define IMAGE_COUNT 20
  4.  
  5.  
  6. //create an array to hold the images
  7.  
  8. NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];
  9.  
  10. //build array of images, cycling through image names
  11. int i;
  12. for(i=1; i<IMAGE_COUNT; i++)
  13. [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d.png", i]]];
  14.  
  15. //another short example:
  16. for (int c=0;c<300;c++)
  17. {
  18. NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
  19. [UIImage imageNamed: imageName];
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.