[英]How to decide whether to assign image to UIImageView or not, once it finish downloading using SDWebImage?
SDWebImage
是UIImageView
的擴展。 有一種方法:
sd_setImageWithURL(NSURL(string: url), placeholderImage: placeholder, options: options, completed: { image, error, cache, url in
//do sth after it is completed
})
但是在completed
塊中,我無法決定是否允許SDWebImage
分配圖像。 它是自動分配的,無需詢問開發人員是否允許這樣做。 當我在滾動表時將圖像分配給UITableViewCell
UIImageView
時出現問題。
顯然,在完成下載之前,該單元可能會重用於不同的行,最后可能會有不同的映像。
如何使用此庫修復它?
在以下問題中: 從UITableView單元內的URL加載異步圖像-滾動時圖像變為錯誤圖像 ,有一個答案是SDWebImage
解決了此問題,但這不是真的,或者我錯過了。
這不是必須的。 如果查看UIImageView類別的實現,您將看到以下內容:
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
[self sd_cancelCurrentImageLoad];
objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
這說明您在調用setImage的那一刻取消了與圖像視圖實例關聯的下載。 由於UITableView重用了其視圖,因此這應該不是問題。 了解他們如何將imageView和URL關聯在一起。
您的問題可能是您沒有將imageView的image屬性每次都重置為nil。 因此,在cellForRow內部,您可能想要:
cell.imageView.image = nil
cell.imageView.sd_setImageWithURL(...)
當您開始新的加載時,圖像視圖會取消任何當前加載。 因此,如果重新使用圖像視圖,則可以避免此問題。
如果由於某種原因沒有設置新圖像,則可以調用sd_cancelCurrentImageLoad
並顯式設置占位符圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.