iOS SDWebImageのprogressBlockでexpectedsize=0の問題

1123 ワード

今日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
           ...
            
        }];

その結果、progressBlockのexpectedSizeは常に0であることが分かった.
これはNSHTTPURLResponseにおけるAccept-EncodingがgzipであることによるAccept-Encodingがgzipであるとexpectedsizeが-1の不確定な大きさになるこのときSDWebImageにおいてexpectedsizeが0より小さいと判断すると0が付与されるためである
解決方法:Accept-Encodingを非gzipに変更すれば必要なファイルサイズを取得できる
        [[SDWebImageDownloader sharedDownloader] setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];