需要做如下处理:在APPdelegate的didFinishLaunchingWithOptions方法中添加如下对SDWebImage的配置
- (void)SDWebImageSetup {
// SDWebImage 缓存配置
SDWebImageDownloader *imgDownloader = SDWebImageManager.sharedManager.imageDownloader;
// 获取SDWebImage的下载对象,所有图片的下载都是此对象完成的 SDWebImageDownloader *imgDownloader = SDWebImageManager.sharedManager.imageDownloader;
imgDownloader.headersFilter = ^NSDictionary *(NSURL *url, NSDictionary *headers) {
// 下载图片成功后的回调
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *imgKey = [SDWebImageManager.sharedManager cacheKeyForURL:url];
NSString *imgPath = [SDWebImageManager.sharedManager.imageCache defaultCachePathForKey:imgKey];
// 获取当前路径图片服务端返回的请求头相关信息
NSDictionary *fileAttr = [fm attributesOfItemAtPath:imgPath error:nil];
NSMutableDictionary *mutableHeaders = [headers mutableCopy];
NSDate *lastModifiedDate = nil;
// 大于0则表示请求图片成功
if (fileAttr.count > 0) {
if (fileAttr.count > 0) {
// 如果请求成功,则手机端取出服务端Last-Modified信息
lastModifiedDate = (NSDate *)fileAttr[NSFileModificationDate];
}
}
// 格式化Last-Modified
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
NSString *lastModifiedStr = [formatter stringFromDate:lastModifiedDate];
lastModifiedStr = lastModifiedStr.length > 0 ? lastModifiedStr : @"";
// 设置SDWebImage的请求头If-Modified-Since
[mutableHeaders setValue:lastModifiedStr forKey:@"If-Modified-Since"];
return mutableHeaders;
};
}
在需要使用的地方,对imageView进行SDWebImageRefreshCached操作,如:[_headerpicView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.userinfodict[@"userpic"]]] placeholderImage:[UIImage imageNamed:@"my_head_default"] options:SDWebImageRefreshCached];
网友评论