iOS 开发每天分享优质文章iOS学习

SDWebImage加载cell图片不显示

2016-12-15  本文已影响0人  djing

    最近项目中发现一个问题, 我做的是tableview的cell里面加了一个collectionview,使用collection view的cell来加载图片,由于之前写代码的原因,是给cell image,让cell来显示图片,使用SDWebImage的如下方法

[imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:img];

然后

cell.img = imgView.image;

发现图片不能自动刷新成为下载好的图片,一直显示的占位图片.只有滚动列表,或者刷新collectionview才能显示图片,但是我的项目里有的位置不能刷新,图片就一直得不到显示.

后来使用这个方法:

[imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:img options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

HXNSlog(@"进来了");

dispatch_async(dispatch_get_main_queue(), ^{

cell.img = image;

});

}];

在SDWebImage的下载完成回调后,刷新cell的图片.发现回调方法一直不进.

但是图片还是一直不显示,后来参考了下面衔接:

http://blog.csdn.net/codingfire/article/details/52640997

发现是因为imgView被释放了,进入不了 下载完成的回调,于是写成如下的样子:

//要强引用imgview不然无法回调,无法刷新图片

[self.imgViewArr addObject:imgView];

//下载图片

//        [imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:img];

[imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:img options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

HXNSlog(@"进来了");

dispatch_async(dispatch_get_main_queue(), ^{

cell.img = image;

});

}];

最后成功显示图片.

记录下来,免得以后遇见这样的坑.

上一篇下一篇

猜你喜欢

热点阅读