2

我有一个模拟封面流的集合视图。它像这样从 ALAssetLibrary 异步加载图像。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    myCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CELL_ID" forIndexPath:indexPath];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                       ^{
                           ALAsset *asset = [assets objectAtIndex:indexPath.row];
                           CGImageRef thumbnailImageRef = [[asset defaultRepresentation]fullScreenImage];
                           UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
                           dispatch_async(dispatch_get_main_queue(), ^{
                               cell.myImageView.image = thumbnail;
                           });
                       });

    return cell;
}

如果用户快速滚动,则加载图像需要更长的时间。我在想,如果用户快速滚动到图像 20,他们必须等待图像 1-19 加载,即使它们不再在屏幕上。所以,我的问题是,如果单元格在加载过程完成之前移出屏幕,有没有办法停止图像加载?

4

1 回答 1

1

一旦调度块排队,就不可能取消它们。我已经使用NSOperationQueue. 将您的调度调用替换NSOperation为具有标识它们属于哪个索引路径的属性的子类,并将它们放入操作队列中。然后,在滚动委托回调期间,遍历队列,取消距离太远的操作。

于 2012-12-20T19:01:45.973 回答