What happens to SDWebImage Cached Images in my app when the image file on the server changes?

后端 未结 10 1192
夕颜
夕颜 2020-12-07 09:57

I am using the SDWebImage library to cache web images in my app:

https://github.com/rs/SDWebImage/blob/master/README.md

Current Usage:



        
相关标签:
10条回答
  • 2020-12-07 10:20

    SDWebImage does aggressive caching by default. But now they give the option to respect your HTTP caching control headers and get the latest image.

    For this they have a new method where in options you can pass SDWebImageRefreshCached

    [imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/xyz/picture"]
                 placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                          options:SDWebImageRefreshCached];
    

    You can find the complete method details and explanation here.

    0 讨论(0)
  • 2020-12-07 10:21
        NSURL *imageUrl = nil;
        NSDate *lastUpdate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastUpdate"];
        NSDate *currentDate = [NSDate date];
    
        if (lastUpdate == nil 
            || ![lastUpdate isKindOfClass:[NSDate class]] 
            || [currentDate timeIntervalSinceDate:lastUpdate] > 60 * 60 *24) {
                [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastUpdate"];
                NSString *urlString = [NSString stringWithFormat:@"http://yourdomain.com/image/image.png?%f", [currentDate timeIntervalSince1970]];
                imageUrl = [NSURL URLWithString:urlString];
        }
    
    0 讨论(0)
  • 2020-12-07 10:25

    Below is what I have observed.

    1. If the image name/ path is same, SDWebImage will not download it again for 1 week.

    2. Irrespective of Image name, SDWebImage will re-download the image after 1 week (from the time it is downloaded).

      static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week

    3. They have one Database where all images URL are stored. For them, image URL is like primary key (unique key).

    So basically what they do is if the URL is changed and not present in DB, download it.

    From my point of view what they are doing is RIGHT. Ex. If you upload image let's say for user A, the image name has to be changed & this is basic. I know some developer prefer image name to be same (like userA.png always).

    0 讨论(0)
  • 2020-12-07 10:33

    Latest Swift 3.0* and SDWebImage

    SDWebImageManager.shared().imageCache?.deleteOldFiles(completionBlock: nil)
    
    0 讨论(0)
提交回复
热议问题