如何限制SDImageCache的磁盘和内存缓存的大小

Uts*_*sad 2 caching ios sdwebimage

我想限制 SDImageCache 的内存缓存和磁盘缓存的大小。下面是我用来下载图像的代码。默认情况下,它将图像保存在缓存中,并且存在于沙箱的库文件夹中。

我的应用程序 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;
    }
Run Code Online (Sandbox Code Playgroud)

Bil*_*lal 6

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)
Run Code Online (Sandbox Code Playgroud)

雨燕4

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

// Setting memory cache
SDImageCache.shared().maxMemoryCost = 15 * 1024 * 1024
Run Code Online (Sandbox Code Playgroud)

  • 它现在在配置中定义:`SDImageCache.shared.config.maxMemoryCost` (2认同)