【问题标题】:Lazy loading images in UICollectionView with SDWebImageManager使用 SDWebImageManager 在 UICollectionView 中延迟加载图像
【发布时间】:2014-02-03 16:15:12
【问题描述】:

在我的方法 cellForItemAtIndexPath 我有这个代码:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:coverURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
    RRRBigItemCell *cell = (RRRBigItemCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell) {
        RRRImageView *coverView = (RRRImageView *)[cell viewWithTag:COVER_TAG];
        coverView.image = image;
    }
}];

当我启动应用程序并向下滚动时,一切正常。但是当我向上滚动时,已经显示的单元格没有加载图像(单元格 == nil)。如果我再次向下滚动,问题仍然存在于已显示的单元格中,只有新的单元格已加载图像。 知道我做错了什么吗?

【问题讨论】:

    标签: ios objective-c lazy-loading uicollectionview sdwebimage


    【解决方案1】:

    @pawan 是正确的,这将是实现 SDWebImage 的最简单方法。它基本上是一个UIImage的类别,所以只需使用类别方法(setImageWithURL)。

    你问的其他几点;

    1. 在您的代码中,您应该在主线程上设置coverImage.image,而不是在后台。

    2. 最好检查 Cell 是否可见,否则显示内容没有意义。

    3. 最好在访问单元格时创建对 Collection 视图的弱引用以避免循环引用

    所以你的代码)可能会写成如下,如果你坚持按照你的方式去做(使用类别是最好和最简单的方法)

      __weak myCollectionViewController *weakSelf = self;
      SDWebImageManager *manager = [SDWebImageManager sharedManager];
      [manager downloadWithURL:[NSURL URLWithString:coverURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    
      } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
    
         dispatch_async(dispatch_get_main_queue(), ^{
            if ([weakSelf.collectionView.indexPathsForVisibleItems containsObject:indexPath]) {
               RRRBigItemCell *cell =  (RRRBigItemCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath];
               RRRImageView *coverView = (RRRImageView *)[cell viewWithTag:COVER_TAG];
               coverView.image = image;
            };
         });  
    
     }];
    

    【讨论】:

    • 好吧,甚至在他写下他的答案之前,我就使用了 pawan 的解决方案。问题是 RRRImageView 它不仅仅是 UIImageView 的子类,所以使用这个类别有点复杂,但不知何故我设法做到了。我之前也尝试过您的解决方案,但 indexPathsForVisibleItems 不包含 indexPath。也许是因为我没有使用主线程?任何的想法?代码完全相同,但没有调度......
    • Apple 确实建议在主线程上更新 UI,我当然读过一些实例,在后台线程上更新 UI 时,collectionviews 和 tableviews 会出现巨大延迟。 indexPathsForVisibleItems 将返回一个数组,并将包含可见项目的 indexPaths 集合。
    【解决方案2】:

    你为什么不试试这个 [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    参考SDWebImage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2014-08-27
      • 2010-10-06
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多