简体   繁体   English

SDWebImage不更新缓存中的图像

[英]SDWebImage not updating image in cache

I am using SDWebImage library for handling image cache. 我正在使用SDWebImage库来处理图像缓存。 I implemented one of its method to download image from server and populating data in tableView . 我实现了一种从服务器下载图像并在tableView填充数据的方法。 Here is my code for downloading image and showing in tableViewCell In cellForRowAtIndexPath , I did the following code 这是我下载图像的代码并在tableViewCell显示在cellForRowAtIndexPath ,我做了以下代码

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

It works perfectly here. 它在这里工作得很好。 But when I updated image on server, the tableViewCell still shows the same image. 但是当我在服务器上更新图像时, tableViewCell仍然显示相同的图像。 The problem is ,its cache is not updating image. 问题是,它的缓存不是更新图像。 I searched with the same keywords and came across this question on StackOverflow. 我搜索了相同的关键字,并在StackOverflow上遇到了这个问题。 But couldn't resolve the issue. 但无法解决问题。 I also tried to clear memory and cache on viewDidDisappear 我还尝试清除viewDidDisappear上的内存和缓存

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [[SDImageCache sharedImageCache]clearMemory];
    [[SDImageCache sharedImageCache]clearDisk];
}

But its not efficient way. 但它效率不高。 Is there any other way to update the cache when image on server is updated ? 当服务器上的映像更新时,还有其他方法可以更新缓存吗?

You can use SDWebImageRefreshCached option to refresh concrete image, however you never know if the image was changed on the server side. 您可以使用SDWebImageRefreshCached选项刷新具体图像,但是您永远不知道图像是否在服务器端更改。 So it's only your decision should you use image cache or not and when your cache should be updated. 因此,只有您决定是否使用图像缓存以及何时应更新缓存。

From SDWebImage docs : 来自SDWebImage 文档

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. 如果您不控制您正在使用的图像服务器,则可能无法在更新其内容时更改URL。 In such case, you may use the SDWebImageRefreshCached flag. 在这种情况下,您可以使用SDWebImageRefreshCached标志。 This will slightly degrade the performance but will respect the HTTP caching control headers 这会稍微降低性能,但会尊重HTTP缓存控制头

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

It would defeat the purpose of Image Caching if you use this in all your code. 如果在所有代码中使用它,它将失去图像缓存的目的。 Coz the image will be downloaded everytime. 因为每次都会下载图像。 Instead you could : 相反,你可以:

  1. If you manage the server, you could setup some flags in the DB to indicate the image has changed on the server. 如果您管理服务器,则可以在数据库中设置一些标志以指示服务器上的映像已更改。
  2. Implement some logic to only use SDWebImageRefreshCached once in 2 days or something like that, and use the normal code all other times. 实现一些逻辑,只在2天内使用一次SDWebImageRefreshCached或类似的东西,并在其他时间使用普通代码。

You could also clear the entire cache once in a while to force a refresh, if that's feasible. 如果可行,您还可以偶尔清除整个缓存以强制刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM