1

I'm using GCD to load images for my scroll view but rather than following the frames i created it makes only a single frame in which the images just flicker through. Here's my code:

//Create the array    
-(void)awakeFromNib {

images = [NSMutableArray arrayWithObjects: [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageOne" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageTwo" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageThree" ofType:@"png"]],nil];

}

 //Populate scrollview
-(void)populate {

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);


scrollview.pagingEnabled = YES;
scrollview.backgroundColor = [UIColor clearColor];
scrollview.clipsToBounds = YES;
scrollview.delegate = self;

[scrollview setContentSize:CGSizeMake(2 * scrollview.frame.size.width, scrollview.frame.size.height)];

int number = [images count] ;
NSLog(@"%i",images);

int rowTwoInt = 0;
int rowThreeInt = 0;
int rowFourInt = 0;

for (int i = 0; i < number; i++) {



    if (i <= 3) {

        finalFrame = CGRectMake( i*(80), 12, 80, 80);
    }

    if (i == 4) {
        finalFrame = CGRectMake(0, 95, 80, 80);
    }
    if ((i >4) && (i <= 7)) {
        rowTwoInt = rowTwoInt + 80;
        finalFrame = CGRectMake(rowTwoInt, 95, 80, 80);
    }

    if (i == 8) {
         finalFrame = CGRectMake(0, 178, 80, 80);
    }
    if ((i > 8) && (i <= 11)) {
        rowThreeInt = rowThreeInt + 80;
        finalFrame = CGRectMake(rowThreeInt, 178, 80, 80);
    }


    if (i == 12) {
        finalFrame = CGRectMake(0, 261, 80, 80);
    }
    if ((i > 12) && (i <= 15)) {
        rowFourInt = rowFourInt + 80;
        finalFrame = CGRectMake(rowFourInt, 261, 80, 80);
    }


    IconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    IconButton.frame = CGRectMake(0, 0, 57, 57);
    IconButton.center = CGPointMake(40, 29);

    [IconButton addTarget:self action:@selector(Selected:) forControlEvents:UIControlEventTouchUpInside];
    IconButton.tag = i;



    dispatch_async(queue, ^{
        UIImage *icon = [images objectAtIndex:i];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [IconButton setImage:icon forState:UIControlStateNormal];

        });
    });



    [cell addSubview:IconButton];


      cell = [[UIView alloc] initWithFrame:finalFrame];
 [scrollview addSubview:cell];

So what am i doing wrong? I'm new with GCD so it's probably something with that. Also if you see a way to improve UIScrollView performance, please let me know.

1
  • Please fix your code to be properly indented. Also, show us how IconButton is declared.
    – rob mayoff
    Apr 5, 2013 at 23:51

1 Answer 1

2

I suspect you have declared IconButton as an instance variable, a static variable, or a global variable.

Each time through your loop, you change the value of IconButton. By the time your twelve queue blocks execute, IconButton is set to its final value - a reference to the last button you created. All the blocks use the instance (or static or global) IconButton variable, so all the blocks update the same, final button.

Make a local variable to reference the button, and use that local variable in the loop:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
IconButton = button;
IconButton.frame = CGRectMake(0, 0, 57, 57);
...

dispatch_async(queue, ^{
    UIImage *icon = [images objectAtIndex:i];

    dispatch_sync(dispatch_get_main_queue(), ^{
        [button setImage:icon forState:UIControlStateNormal];

    });
});

Each block will capture the value of the local variable at the moment the block is created, so each block will capture a reference to a different button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.