? ? 最近項目中發(fā)現(xiàn)一個問題, 我做的是tableview的cell里面加了一個collectionview,使用collection view的cell來加載圖片,由于之前寫代碼的原因,是給cell image,讓cell來顯示圖片,使用SDWebImage的如下方法
[imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:img];
然后
cell.img = imgView.image;
發(fā)現(xiàn)圖片不能自動刷新成為下載好的圖片,一直顯示的占位圖片.只有滾動列表,或者刷新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的下載完成回調(diào)后,刷新cell的圖片.發(fā)現(xiàn)回調(diào)方法一直不進.
但是圖片還是一直不顯示,后來參考了下面銜接:
http://blog.csdn.net/codingfire/article/details/52640997
發(fā)現(xiàn)是因為imgView被釋放了,進入不了 下載完成的回調(diào),于是寫成如下的樣子:
//要強引用imgview不然無法回調(diào),無法刷新圖片
[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;
});
}];
最后成功顯示圖片.
記錄下來,免得以后遇見這樣的坑.