我想限制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;
}
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
如果您希望限制缓存的最大大小,可以在 SDImageCache.m 中手动执行此操作,并将
maxCacheSize
设置为有限的字节数。您可以使用 static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
从 v5 开始,您可以这样配置缓存:
SDImageCache.shared.config.maxMemoryCost = 10_000_000 // 10 MB
SDImageCache.shared.config.maxDiskSize = 100_000_000 // 100 MB