Supertecnoboff
Supertecnoboff

Reputation: 6606

Simple For loop issue

I have a two for loops in my Xcode project which are using to change the image in 9 UIImageViews. The images in these UIImageViews is downloaded from a server and then presented.

My for loops use 3 different integers to figure out what image to show: next, current_photo and previous are integers.

I have two UIButtons which control showing the next and previous set of images.

When I want to show the next set of images, I made and use the following for loop:

NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];

        for (next = current_photo; next < (current_photo+9); next++) {
            NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, next+1, FORMAT_TYPE];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[imageViews objectAtIndex:(next-9)] setImage:image];
        }

        current_photo = next;

This for loop works perfectly and all 9 UIImagesViews change image.

However, when I want to show the previous set of images in the 9 UIImageViews, the following for loop does NOT work properly for some reason:

NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];

        for (previous = current_photo; previous > (previous-9); previous--) {
            NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[imageViews objectAtIndex:(previous-previous)] setImage:image];
        }

        current_photo = previous;

What is wrong with my for loop? Please explain.

Below is what happens when my app is opened: Opened App

Below is what happens when the next button is pressed:

Next

And finally when pressing the back button the app just freezes.....

Why? Whats wrong? Please help.

Thanks for your time :)

Upvotes: 1

Views: 95

Answers (1)

HalR
HalR

Reputation: 11073

For one thing:

[[imageViews objectAtIndex:(previous-previous)] setImage:image];

will always be setting the image at [0] and none of the others will have anything set for them.

Also

(previous = current_photo; previous > (previous-9); previous--) 

will loop forever! Every time you do a previous--, the thing you are comparing against to know when to stop, previous-9 also goes down one.

I would recommend this:

for (previous = current_photo; previous > (current_photo-9); previous--) {
    NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE];
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    [[imageViews objectAtIndex:(8 - (current_photo - previous))] setImage:image];
}

Upvotes: 1

Related Questions