【问题标题】:How to limit size of image disk cache using AFNetworking如何使用 AFNetworking 限制图像磁盘缓存的大小
【发布时间】:2017-06-15 20:07:35
【问题描述】:

在我们的应用程序中,我们通过 Cocapods(当前版本 3.1.0)使用 AFNetorking 进行图像下载,并且我们使用磁盘缓存,它正在工作,但似乎磁盘上的缓存大小没有限制,我没有不知道怎么设置。

文档说:

AFNetworking 已经利用了缓存功能 由 NSURLCache 及其任何子类提供。只要你的 NSURLRequest 对象具有正确的缓存策略,并且您的服务器 响应包含有效的 Cache-Control 标头,响应将是 为后续请求自动缓存。

我们的图像响应具有这样的缓存控制标头:

缓存控制:公共,max-age=31209907

我们正在使用此代码来获取图像(明确设置缓存策略):

- (void)setImageWithURLString:(NSString *)URLString success:(void (^)())success
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    [request addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

    __weak __typeof(self)weakSelf = self;
    [self setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (strongSelf && success) {
            strongSelf.image = image;
            success();
        }
    } failure:nil];
}

磁盘容量默认设置为 150 MB(我在 AFNetworking 中看到):

+ (NSURLCache *)defaultURLCache {
    return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
                                         diskCapacity:150 * 1024 * 1024
                                             diskPath:@"com.alamofire.imagedownloader"];
}

我正在尝试将其设置为 100 MB,如下所示:

AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance];
NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;
urlCache.diskCapacity = 100 * 1024 * 1024;

如果我让默认值 150 MB,容量将被忽略。此外,如果我将磁盘容量设置为 100 MB,则该值将被忽略。 我可以通过访问currentDiskUsage 来确认这一点。在任何情况下,它都远远超出diskCapacity

我基本上有两个问题:

  1. 我的设置是否遗漏了什么?
  2. 是否可以设置磁盘容量,让缓存有限制?

提前致谢:)

【问题讨论】:

    标签: caching afnetworking nsurlcache


    【解决方案1】:

    截至 AFNetworking 3.1:

    AFImageDownloader -init 使用AFImageDownloader +defaultURLSessionConfiguration 创建AFHTTPSessionManager。在内部,AFHTTPSessionManager(和AFURLSessionManager)使用AFImageDownloader +defaultURLSessionConfiguration 创建自己的NSURLSession,该AFImageDownloader +defaultURLSessionConfiguration 已配置为使用硬编码20/150 的AFImageDownloader +defaultURLCache

    见:https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration?language=objc

    2018 年 1 月 17 日更新

    截至 AFNetworking 3.2.0:

    可以配置AFImageDownloaderNSURLSessionConfigurationNSURLSession被创建之前:

    AFImageDownloader 只有磁盘 NSURLCache

    // Force AFNetworking to NOT use any RAM whatsoever for image downloading
    // (neither backed by NSURLCache nor imageCache). We will solely rely on
    // NSURLCache backed by disk
    // (i.e. won't use AFAutoPurgingImageCache and NSURLCache.memoryCapacity=0 disk=300)
    NSURLCache* imageDownloaderCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:300 * 1024 * 1024 diskPath:@"com.alamofire.imagedownloader"];
    
    NSURLSessionConfiguration* imageDownloaderSessionConfiguration = [AFImageDownloader defaultURLSessionConfiguration];
    imageDownloaderSessionConfiguration.URLCache = imageDownloaderCache;
    AFImageDownloader* imageDownloader = [[AFImageDownloader alloc] initWithSessionConfiguration:imageDownloaderSessionConfiguration];
    imageDownloader.imageCache = nil;
    
    // If in the future, we wanted to cache thumbnails in RAM, our own P2AFAutoPurgingImageCache shall do the trick
    //    imageDownloader.imageCache = [[P2AFAutoPurgingImageCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 preferredMemoryCapacity:4 * 1024 * 1024];
    
    [UIImageView setSharedImageDownloader:imageDownloader];
    

    仅缓存缩略图的可选P2AFAutoPurgingImageCache

    @interface P2AFAutoPurgingImageCache : AFAutoPurgingImageCache
    @end
    
    @implementation P2AFAutoPurgingImageCache
    
    -(BOOL)shouldCacheImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier
    {
        // Only cache thumbnails
        return image.size.width <= 200 && image.size.height <= 200;
    }
    
    @end
    

    详情请看:https://github.com/AFNetworking/AFNetworking/pull/4010

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2016-08-23
      • 2015-02-07
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2014-07-03
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多