项目里个人资料有个更换头像的功能,之前有整过这种功能,但是现在一直不能更换成功,最后才注意到不管怎么更换头像,头像的url是不会变的,上网搜解决办法记录如下:
头像不更新的原因:原因: SDWebImage缓存图像会优先从内存读取用户头像,如果内存中没有会从沙盒(也就是硬盘)中读取,如果沙盒中也没有,才会异步从网络上请求头像,如果头像已经存在沙盒或者内存中,SDWebImage就不会从网络上请求,而是加载本地的,会导致头像无法更新。
解决办法:
加载头像的时候用下面的方法
sd_setImageWithURL:<#(NSURL *)#> placeholderImage:<#(UIImage *)#> options:<#(SDWebImageOptions)#> //options用:SDWebImageRefreshCached
同时更改sdwebimage框架里的源码
把
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 is cached but force refreshing
downloaderOptions |=SDWebImageDownloaderIgnoreCachedResponse;
}
这样就可以解决头像无法更新的问题了。