SDWebImage 不更新缓存中的图像

SDWebImage not updating image in cache

我正在使用 SDWebImage 库来处理图像缓存。我实现了其中一种从服务器下载图像并在 tableView 中填充数据的方法。这是我下载图片并显示在 tableViewCell 中的代码 在cellForRowAtIndexPath中,我做了以下代码

[cell.profileImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRefreshCached];

它在这里完美运行。但是当我在服务器上更新图像时,tableViewCell 仍然显示相同的图像。问题是,它的缓存没有更新图像。我使用相同的关键字搜索并在 Whosebug 上遇到了 问题。但是无法解决问题。我还尝试在 viewDidDisappear

上清除内存和缓存
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [[SDImageCache sharedImageCache]clearMemory];
    [[SDImageCache sharedImageCache]clearDisk];
}

但这不是有效的方法。更新服务器上的图像时,还有其他方法可以更新缓存吗?

您可以使用 SDWebImageRefreshCached 选项来刷新具体图像,但是您永远不知道图像是否在服务器端被更改。因此,是否使用图像缓存以及何时更新缓存完全由您决定。

来自 SDWebImage docs :

If you don't control the image server you're using, you may not be able to change the URL when its content is updated. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers

[imageView sd_setImageWithURL:url
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      options:SDWebImageRefreshCached];

如果您在所有代码中都使用它,则会破坏 图像缓存 的目的。因为每次都会下载图像。相反,您可以:

  1. 如果您管理服务器,您可以在数据库中设置一些标志以指示服务器上的图像已更改。
  2. 实施一些逻辑以在 2 天内或类似情况下仅使用 SDWebImageRefreshCached 一次,并在所有其他时间使用正常代码。

如果可行,您也可以偶尔清除整个缓存以强制刷新。