UICollectionView的延迟加载图像问题

时间:2014-02-27 12:28:17

标签: ios iphone objective-c xcode

我正在为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;

}

2 个答案:

答案 0 :(得分:-1)

我建议您使用第三方库来延迟加载图像,例如this one,它也支持网络缓存。
另外,考虑objective-c中使用的约定:
- 避免使用snake_casing,使用camelCase
- 广泛使用房产
- 使用大写的类名

答案 1 :(得分:-1)

嘿,这是Apple开发者网站上的一个非常好的例子。

这是is an example code,其中apple演示了一种在表中进行延迟加载的方法。参考这个可能会以某种方式帮助你。 一切顺利