2013-04-05 55 views
1

我正在使用GCD爲我的滾動視圖加載圖像,而不是跟隨我創建的框架,這使得圖像只閃爍通過一幀。這裏是我的代碼:GCD滾動查看加載問題

//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]; 

那我做錯了什麼?我是GCD的新手,所以它可能就是這樣。另外,如果您看到一種提高UIScrollView性能的方法,請告訴我。

+0

請修復您的代碼以正確縮進。另外,告訴我們如何聲明'IconButton'。 – 2013-04-05 23:51:14

回答

2

我懷疑你已經聲明IconButton作爲實例變量,static變量或全局變量。

每次通過循環時,都會更改IconButton的值。當您執行12個隊列塊時,IconButton被設置爲其最終值 - 對您創建的最後一個按鈕的引用。所有塊都使用實例(或static或全局變量),因此所有塊都會更新相同的最終按鈕。

使一個局部變量來引用按鈕,和使用,在循環局部變量:

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]; 

    }); 
}); 

每個塊將在塊產生的力矩捕獲局部變量的值,所以每個塊將捕獲對不同按鈕的引用。