0
votes

I'm trying to display 34 consecutive images in a UIImageView container when a button is pressed.

- (IBAction)buttonPressed {
    [self updateUI:(0)];
}

-(void)updateUI:(int)yesser{

    //if GO is pressed

        //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files
        for(int j = 0;j<34;j++){
            NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i.PNG",42+j];
            NSLog(@"%@",newPhoto);
            sleep(1);
            [self setNextImage:newPhoto];
        }
}

-(void)setNextImage:(NSString*)nextImage{

    [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES];

}

However, images are not updating. When all the code finishes and returns, the first image is displayed.

I've tried the following:
1) Product > Clean
2) Reinstalled app.
3) Checked all case sensitivity.
4) Tested number of methods for setting UIImageView images.
5) Checked if 34 static NSString values worked instead of changing one NSString each time
6) Removed and re-added images to supporting files folder.
7) Renamed all images and updated code to match.
8) Turned AutoLayout off.

Any help would be GREATLY appreciated.

Thanks, Rob

3
I can only imagine that using sleep() would cause you more issues than help. Couldn't you just use an NSTimer and schedule it to call setNextImage: every second? - SpacePyro
Show your setImage method - Samkit Jain
I have a sneaking suspicion that you are trying to replicate animation using sleep.. - amar

3 Answers

0
votes

I think you'd generally want to avoid using sleep() if you're trying to update the UI. When you invoke sleep, you're telling the main thread to pause for a certain period of time. Since the UI is all done on the main thread, I can imagine that as it's trying to update it's view, your calling of sleep() prevents it from fully updating. Hence, why you might not be seeing any updates until the very end.

As I mentioned in my comment, since you want to update what I imagine is a UIImageView, you should consider using an NSTimer. This class is convenient for wanting to schedule a periodic update on a certain time interval. In this case, we want the UIImageView to start updating the minute buttonPressed is called, so we could do something like this:

- (IBAction)buttonPressed:(id)sender {
    [self updateUI];
}

- (void)updateUI{
    // If you wanted to keep track of this NSTimer, you could hold onto it 
    // via a property.
    // `fire` basically tells the timer to begin executing what's been set 
    // for the selector.
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setNextImage:) userInfo:nil repeat:YES];
    [timer fire];
}

- (void)setNextImage {
        // Here is where your logic would change. You would need some kind of way 
        // of figuring out which image to display. You could have an NSInteger 
        // property store how many times the method gets called and then 
        // determine which image to show from there, for example.

        // Assume that with your new logic, you have acquired the image you 
        // want to display.
        UIImage *image = imageToDisplay;

        // Also assuming here that (based on your code), `ball` is the 
        // name of your UIImageView.
        ball.image = imageToDisplay;
}

To destroy the timer, simply call [timer invalidate].

0
votes

UIImageView has an option for animating images, try this

imageView.animationImages = [NSArray arrayWithObjects:
                                        [UIImage imageNamed:@"progress-1"],
                                        [UIImage imageNamed:@"progress-2"],
                                        [UIImage imageNamed:@"progress-3"],
                                        [UIImage imageNamed:@"progress-4"], nil];
imageView.animationDuration = 2.0f;
imageView.animationRepeatCount = 1;
[imageView startAnimating];

you have to rename the images in sequential order and call this on button press..

-1
votes

Try this

- (IBAction)buttonPressed {
    [self updateUI:(0)];
}

-(void)updateUI:(int)yesser{

//if GO is pressed

    //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files
    for(int j = 0;j<34;j++){
        NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i",42+j];
        NSLog(@"%@",newPhoto);
        sleep(1);
        [self setNextImage:newPhoto];
    }
}

-(void)setNextImage:(NSString*)nextImage{

     [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES];
     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
     imgView.image = [UIImage imageNamed:[NSString stringWithFormat: @"%@.png", nextImage]];
    [self.view addSubview:imgView];
}