3

I got an NSArray with 5 pictures. I want to display them one after the other in a loop. My code :

 NSArray *tabImage = [[NSArray alloc] init];

tabImage = [[NSArray arrayWithObjects:  
               [UIImage imageNamed:@"picto_1.png"],
               [UIImage imageNamed:@"picto_2.png"],
               [UIImage imageNamed:@"picto_3.png"],
               [UIImage imageNamed:@"picto_4.png"],
               [UIImage imageNamed:@"picto_5.png"],
               nil] retain];

int var = 0;
var = indexPath.row;

for (int var = 0; var < 20; var++) {
    pictoImage.image = [tabImage objectAtIndex:(var % 5)];
}

I got the same picture all the time.

Thanks for help.

1

4 Answers 4

6

I’m guessing pictoImage is an UIImageView. In that case look at the class reference, specifically the animationImages properties.

 pictoImage.animationImages = [NSArray arrayWithObjects:  
                                   [UIImage imageNamed:@"picto_1.png"],
                                   [UIImage imageNamed:@"picto_2.png"],
                                   [UIImage imageNamed:@"picto_3.png"],
                                   [UIImage imageNamed:@"picto_4.png"],
                                   [UIImage imageNamed:@"picto_5.png"],
                                   nil];

 // How many seconds it should take to go through all images one time.
 pictoImage.animationDuration = 5.0;

 // How many times to repeat the animation (0 for indefinitely).
 pictoImage.animationRepeatCount = 4;

 [pictoImage startAnimating];
Sign up to request clarification or add additional context in comments.

Comments

3

You are not giving the framework time to actually render the images to the window.

All your updates are being coalesced into one "setNeedsDisplay" call. On the next iteration of the run loop, the image view will be rendered using the last image that you set in the loop.

3 Comments

How can i do to print each picture ?
If by "print" you mean "render to the window", there's a slew of options. If you insist on doing this synchronously, give the runloop some time to run in between changing the images. See my answer here: stackoverflow.com/questions/5829977/…
Otherwise, use the builtin animation support from Hagelin's answer, do performSelectorAfterDelay with varying delays, etc. All of these will let the run loop run, which means the window gets updated to reflect your changes.
2

If pictoImage is an UIImageView, @Hagelin's answer is the best way to go. Now, if pictoImage is not an UIImageView object then you will have to take a different approach and that definitely is not a loop.

Set an NSTimer with a desired time interval and invoke a method that updates pictoImage iterating over the tabImages. It should be something like this :-

- (void)setup {
    ...

    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(updatePictoImage:)
                                   userInfo:nil
                                    repeats:YES];

    ...
}

- (void)updatePictoImage:(NSTimer*)theTimer {
    static int i = 0;
    if ( i > 20 ) {
        pictoImage.image = [tabImages objectAtIndex:(i % 5)];
    } else {
       [theTimer invalidate];
    }
}

This should update the pictoImage every 2 seconds with images in tabImages.

Comments

0

You don't print anything.

You are overriding the contents of your pictoImage 20 times in a row, leaving it with the last assignment when var is 19, so it should be picto_4.png.

Also, the whole var declaration doesn't make much sense at all... first you set it to 0, then you set it to indexPath.row, and in the loop it is completely redefined....

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.