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

问题描述 投票:0回答:3

我想限制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;
    }
ios caching sdwebimage
3个回答
7
投票

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)

斯威夫特4

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

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

0
投票

如果您希望限制缓存的最大大小,可以在 SDImageCache.m 中手动执行此操作,并将

maxCacheSize
设置为有限的字节数。您可以使用
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week

查看缓存期限如何达到一周 并对缓存大小执行相同的操作。


0
投票

从 v5 开始,您可以这样配置缓存:

SDImageCache.shared.config.maxMemoryCost = 10_000_000 // 10 MB
SDImageCache.shared.config.maxDiskSize = 100_000_000 // 100 MB
© www.soinside.com 2019 - 2024. All rights reserved.