此處對題目再次詳細的解釋一下:我們使用SDWebImage加載圖片,可以說是用起來非常之簡單,它的內(nèi)部已經(jīng)給我們實現(xiàn)了緩存的機制,也正是由于這個緩存機制的原因,當(dāng)后臺更換了圖片的內(nèi)容,但是圖片的URL不變時,我們加載不出來這個新的圖片,顯示的還是之前緩存的圖片;為什么呢?
大致原因就是應(yīng)為SDWebImage將緩存的圖片的URL當(dāng)做key值存入本地,當(dāng)再次取的時候,通過這個key值來獲取到對應(yīng)的圖片,但是圖片的URL沒有變化,最新的圖片沒有緩存下來,所以我們?nèi)〉降膱D片還是之前的;
這里也小小的總結(jié)一下原因:由于之前我加載圖片時用的方法如下(當(dāng)然這個方法就是造成了上面問題的原因,就不要使用了)
[self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
當(dāng)經(jīng)過查看了一些資料之后,發(fā)現(xiàn)用上面的這種方法,加載不出來最新的圖片,所以我改用下面的方法,去解決這種問題:
[self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil options:SDWebImageRefreshCached];
之前的版本這么改是有效的,不過后來就不行了,我也來更新一下這篇文章;除了上面的修改之外,我們還需要在SDWebImage的內(nèi)部,SDWebImageManager.m文件中,大概176行左右吧,把之前的代碼:(如下)
if (image && options & SDWebImageRefreshCached) {
// force progressive off if image already cached but forced refreshing
downloaderOptions &= ~SDWebImageDownloaderProgressiveDownload;
// ignore image read from NSURLCache if image if cached but force refreshing
downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
}
更換成如下代碼:
if (image && options & SDWebImageRefreshCached) {
// force progressive off if image already cached but forced refreshing
downloaderOptions &= ~SDWebImageDownloaderProgressiveDownload;
// remove SDWebImageDownloaderUseNSURLCache flag
downloaderOptions &= ~SDWebImageDownloaderUseNSURLCache;
// ignore image read from NSURLCache if image if cached but force refreshing
downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
}