【问题标题】:How to limit the size of disk and memory cache of SDImageCacheSDImageCache如何限制磁盘和内存缓存大小
【发布时间】:2016-08-23 18:35:03
【问题描述】:

我想限制 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


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      如果您希望限制缓存的最大大小,您可以在 SDImageCache.m 中手动执行此操作,并将 maxCacheSize 设置为有限的字节数。您可以使用
      static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
      查看缓存期限到一周的情况 并对缓存大小执行相同的操作。

      【讨论】:

      • 还有其他方法吗?
      猜你喜欢
      • 2015-02-07
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2018-02-08
      • 2022-12-20
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多