2014-02-27 136 views
2

我正在为UICollectionView延迟加载图像。该图像正在显示,但是当我滚动UICollectionView时,图像会改变它们的位置,并且有重复的图像,所有图像都应该是唯一的。这里是我的代码:延迟加载的图像问题UICollectionView

- (gridcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    gridcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"gallerycollection" forIndexPath:indexPath]; 

    NSDictionary *appsdict = [array_grid objectAtIndex:indexPath.item]; 
    cell.image_grid.image=[UIImage imageNamed:@"griddummyloading"]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 

     NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:@"iosthumbnail"]]; 

     NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 

     UIImage *image = [UIImage imageWithData:imageData]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      cell.image_grid.image=image; 
     }); 
    }); 

    [collectionview_grid setAllowsSelection:YES]; 

    return cell; 

} 
+0

您是从单元格的'prepareForReuse'方法中删除单元格中的图像吗?如果没有,它可能会显示以前使用单元格的图像。 – bobnoble

+0

我的代码我需要改变任何东西? – sures

+0

您是否在uicollectionview中使用了多个部分? –

回答

-1

我建议你使用第三方库来延迟加载图片,就像this one,同时也支持Web缓存。
另外,还要考虑在Objective-C中使用的约定:
- 避免snake_casing,使用驼峰
- 广泛使用性质
的 - 使用上层套管类名

-1

嘿那里有苹果给出一个很好的例子开发者网站。

This is an example code苹果演示了一种在表格中进行延迟加载的方式..提到这可能会以某种方式帮助您。 全部最好