【问题标题】:How to use SDWebImage to cache image for multiple sized images如何使用 SDWebImage 缓存多尺寸图像的图像
【发布时间】:2016-08-01 17:06:48
【问题描述】:

从我的网络服务中,我得到了多个尺寸的图像。我正在将它们加载到 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,在他们的常见问题部分中他们提到了这个问题,但建议的解决方案对我不起作用!!!

【问题讨论】:

  • 单元格中的imageview大小是否固定?
  • 没有图像视图大小是根据图像大小动态变化的

标签: ios image caching sdwebimage


【解决方案1】:

实际上我做了什么,我检查了图像是否已经缓存,如果没有,我已经在 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;

                                       }
                                   }];

        }

【讨论】:

  • 我看不出这个答案有什么用处是处理多尺寸图像的任何方法。
猜你喜欢
  • 2012-10-13
  • 2023-04-03
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2019-01-18
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多