0

I want to display a .gif (so like automatically playing video), therefore I found out I need to use an array of UIImages. Now, I have a video containing 96 images, so I would like to use a loop for naming them. Is that possible?

I tried the following, which does unfortunatly not work:

for (int i = 0; i<97; i++) {
    [UIImage imageNamed:@"%d", i];
}

So what I want to do with the loop is something like this:

NSArray *myArray = [NSArray arrayWithObjects:
                    for (int i = 0; i<97; i++) {
                        [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
                    }
                    , nil];

So is there a way using for-loops in an array? Some ideas?

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174

3 Answers3

1

I'm not sure what exactly you want, but if you want to show animation using a batch of images you could use following approach.

// 1. Create an array of images names. If you have images named 0.png, 1.png, ... 95.png:
NSMutableArray *imageNames = [NSMutableArray new];
for (int i = 0; i < 96; i++)
{
    [imageNames addObject:[NSString stringWithFormat:@"%d", i]];
}

// 2. Create an array of images from array of names:

     NSMutableArray *images = [[NSMutableArray alloc] init];
    [imageNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [images addObject:[UIImage imageNamed:obj]];
    }];

// 3. Assign it to UIImageView:

    self.imageView = [UIImageView new];
    [self.view addSubview: self.imageView];
    self.imageView.animationImages = images;
    self.imageView.animationDuration = 0.5;
    [self.imageView startAnimating];

You can find more at this link

curious
  • 666
  • 4
  • 13
0

imagview have an property ,that is animationImages. you can add your arrayNames to this property.then it works

wpstarnice
  • 21
  • 4
0

Create a property like this in your .h file :

@property(nonatomic, strong) NSMutableArray *images;

initialise it in viewDidLoad method like this :

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.images = [[NSMutableArray alloc] init];
}

You need to do following :

for (int i = 0; i<97; i++) 
{
    UIImage *currentImage = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
    [self.images addObject:currentImage];// images is a NSMutableArray
}

If you want to animate those images, you can do following (Assuming you have a UIImageView object with name imageView) :

imageView.animationImages = self.images;
[imageView startAnimating];

Go through UIImageView class documentation for more options.

Naga Mallesh Maddali
  • 1,005
  • 10
  • 19