使用SDWebImageManager延迟在UICollectionView中加载图像 [英] Lazy loading images in UICollectionView with SDWebImageManager

查看:215
本文介绍了使用SDWebImageManager延迟在UICollectionView中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的方法cellForItemAtIndexPath中,我有以下代码:

In my method cellForItemAtIndexPath i have this code:

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).如果我再次向下滚动,则已经显示的单元格仍然存在问题,只有新单元格才加载图像. 知道我在做什么错吗?

When i launch application and i scroll down then everything is ok. But when i scroll up than cell which were already displayed don't have loaded images (cell == nil). And if i again scroll down the problem remains for already displayed cells, only new ones have loaded images. Any idea what i'm doing wrong?

推荐答案

@pawan是正确的,这是实现SDWebImage的最简单方法.它基本上是UIImage的类别,因此只需使用类别方法(setImageWithURL).

@pawan is correct, that would be simplest way to implement SDWebImage. It's basically a category of UIImage, so simply use the category methods (setImageWithURL).

自您提出问题以来,还有其他几点;

A couple of other points since you asked;

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

  1. In your code, you should be setting the coverImage.image on the main thread, not in the background.

检查单元格是否可见是很好的做法,或者显示内容没有什么意义.

It's good practice to check if the Cell is visible, or there is little point in displaying the content.

优良的做法是在访问单元格时为Collection视图创建一个弱引用以避免循环引用

It's good practice to create a weak reference to the Collection view when accessing the cell to avoid circular references

因此,如果您坚持按照自己的方式进行操作,则代码可以写成如下(使用类别是最好和最简单的方法)

so your code ) might be written as follows if you were insistent on doing it the way you have it (Using the category is the best and simplest way)

  __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;
        };
     });  

 }];

这篇关于使用SDWebImageManager延迟在UICollectionView中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆