Utsav Dusad
Utsav Dusad

Reputation: 2189

How to limit the size of disk and memory cache of SDImageCache

I want to limit the size of memory cache and disk cache for SDImageCache. Below is the code I have used for downloading the images. By default it saves the images in the cache and it is present In the sandbox's Library folder.

my Application ID/Library/Caches/com.hackemist.SDWebImageCache.default/

#import <SDWebImage/UIImageView+WebCache.h>

...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *MyIdentifier = @"MyIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }

        // Here we use the new provided sd_setImageWithURL: method to load the web image
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

        cell.textLabel.text = @"My Text";
        return cell;
    }

Upvotes: 2

Views: 6882

Answers (3)

samwize
samwize

Reputation: 27353

As of v5, you can configure the cache as such:

SDImageCache.shared.config.maxMemoryCost = 10_000_000 // 10 MB
SDImageCache.shared.config.maxDiskSize = 100_000_000 // 100 MB

Upvotes: 0

Bilal
Bilal

Reputation: 19156

Objective-C

// Setting disk cache     
[[[SDImageCache sharedImageCache] config] setMaxCacheSize:1000000 * 20]; // 20 MB

// Setting memory cache 
[[SDImageCache sharedImageCache] setMaxMemoryCost:15 * 1024 * 1024]; // aprox 15 images (1024 x 1024)

Swift 4

// Setting disk cache
SDImageCache.shared().config.maxCacheSize = 1000000 * 20 // 20 MB

// Setting memory cache
SDImageCache.shared().maxMemoryCost = 15 * 1024 * 1024

Upvotes: 7

Nabil
Nabil

Reputation: 1

If you're looking to limit the maximum size of the cache, you can manually do that in SDImageCache.m and set maxCacheSize to a limited number of bytes. You can take a look at how the cache age is being to one week using
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
and do the same for the cache size.

Upvotes: 0

Related Questions