今天使用SDWebImage加載圖片的時候想要一個進度條的加載效果,用的方法如下
[imageView sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil options:SDWebImageCacheMemoryOnly|SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
[SVProgressHUD showProgress:(float)receivedSize/(float)expectedSize];
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if ([SVProgressHUD isVisible]) {
[SVProgressHUD dismiss];
}
//your code
...
}];
結(jié)果發(fā)現(xiàn)progressBlock中的expectedSize始終是0。
這是因為NSHTTPURLResponse中 Accept-Encoding為gzip造成的
當遇到Accept-Encoding為gzip時,expectedsize會變?yōu)?1不確定的大小
此時在SDWebImage中expectedsize判斷小于0,就會賦值為0
解決方法:可以將Accept-Encoding修改成非gzip的就可以獲取需要的文件大小了
[[SDWebImageDownloader sharedDownloader] setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];