在我们的应用程序中,我们通过Cocapods(目前版本3.1.0)使用AFNetorking进行图像下载,我们使用磁盘缓存,它正在工作,但似乎磁盘上的缓存大小没有限制,我不喜欢我不知道如何设置一个。
Doc说:AFNetworking已经利用了缓存功能 由NSURLCache及其任何子类提供。只要你的 NSURLRequest对象具有正确的缓存策略和您的服务器 响应包含一个有效的Cache-Control标头,响应将是 自动缓存后续请求。
我们的图片响应具有缓存控制标头,如下所示:
Cache-Control:public,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
。
我基本上有两个问题:
提前致谢:)
答案 0 :(得分:0)
截至AFNetworking 3.1:
AFImageDownloader -init
使用AFHTTPSessionManager
创建AFImageDownloader +defaultURLSessionConfiguration
。在内部,AFHTTPSessionManager
(和AFURLSessionManager
)使用NSURLSession
创建了自己的AFImageDownloader +defaultURLSessionConfiguration
,AFImageDownloader +defaultURLCache
已经配置为使用带有硬编码的20/150的AFImageDownloader
。
请参阅:https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration?language=objc
更新2018-01-17 :
自AFNetworking 3.2.0开始:
可以在创建NSURLSessionConfiguration
之前配置NSURLSession
的{{1}} :
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