簡體   English   中英

如何使用SDWebImage緩存多個尺寸的圖像

[英]How to use SDWebImage to cache image for multiple sized images

從我的Web服務中,我得到了多個尺寸的圖像。 我正在將它們加載到TableView中。 加載它們時,尚未緩存的新圖像會在占位符和原始圖像之間閃爍,或者有時圖像看起來小於通常的尺寸。 但是只是向下或向上滾動實際上可以解決問題,但是我希望它們從一開始就具有原始大小。 但是我想下一次它將以原始形狀出現,因為圖片已經被緩存了

原來

初始

經過一點滾動

在此處輸入圖片說明

我在cellForRowAtIndexPath中的代碼:

[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]]
                 placeholderImage:[UIImage imageNamed: @"image_loader.gif"]];

然后在heightForRowAtIndexPath中:

NSURL *filePath = [NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]];

        NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:filePath];
        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
        self.img = image;



        if (self.img.size.width > CGRectGetWidth(self.view.bounds)) {
            CGFloat ratio = self.img.size.height / self.img.size.width;
            return CGRectGetWidth(self.view.bounds) * ratio+140;
        } else {

            return self.img.size.height+180;
        }

我訪問了https://github.com/rs/SDWebImage ,在他們的常見問題部分,他們提到了該問題,但是建議的解決方案對我不起作用!!!

實際上我做了什么,我檢查了圖像是否已經緩存,如果沒有,我已經在heightForRowAtIndexPath下異步下載了它:

if(image != NULL)
        {
        self.img = image;
        }
        else
        {
            SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
            [downloader downloadImageWithURL:filePath
                                     options:0
                                    progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                        // progression tracking code
                                    }
                                   completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                       if (image && finished) {
                                           // do something with image

                                           self.img = image;

                                       }
                                   }];

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM