gpt4 book ai didi

ios - NSURLCache 问题 - 图像从缓存中释放?

转载 作者:行者123 更新时间:2023-11-29 12:33:01 28 4
gpt4 key购买 nike

我有一个应用程序,我正在下载很多图像以供稍后显示。

即使没有互联网连接,该应用程序也需要正常运行,因此图像会一次性加载并使用 NSURLCache 持久化。

这是一个巧妙的解决方案,因为我可以使用普通网络库并轻松利用自定义缓存设置。

我意识到这种类型的缓存并不能保证文件在它们过期之前一直存在,因为系统会在它认为必要的时候释放缓存。这不是一个大问题,因为图像应该能够重新下载并因此重新保存。

但是,我注意到它决定从缓存中随机释放图像,并且当我再次下载它们时它似乎不会保留它们。我不确定哪里出错了。

首先,我这样定义缓存容量:

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:50 * 1024 * 1024
diskCapacity:200 * 1024 * 1024
diskPath:@"netcache"];
[NSURLCache setSharedURLCache:URLCache];

(50MB 内存和 200MB 磁盘)

当下载图像(使用 AFNetworking)时,我修改响应 header 以将 Cache-Control 设置为 max-age=31536000 这意味着它应该 将响应缓存一年。

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Download completed
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Download failed
}];

[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
NSURLResponse *response = cachedResponse.response;
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse*)response;
NSDictionary *headers = HTTPResponse.allHeaderFields;

NSMutableDictionary *modifiedHeaders = headers.mutableCopy;
modifiedHeaders[@"Cache-Control"] = @"max-age=31536000"; // 1 year in seconds
NSHTTPURLResponse *modifiedHTTPResponse = [[NSHTTPURLResponse alloc]
initWithURL:HTTPResponse.URL
statusCode:HTTPResponse.statusCode
HTTPVersion:@"HTTP/1.1"
headerFields:modifiedHeaders];

return [[NSCachedURLResponse alloc] initWithResponse:modifiedHTTPResponse data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
}];

[self.operationQueue addOperation:operation];

...然而,图像似乎已发布,再次下载时甚至不会“重新缓存”。 (我相信它们被报告为缓存,即使在设备没有任何连接时尝试显示它们时它们不会加载。)

稍后使用 AFNetworking 的 UIImageView+AFNetworking.h 类别显示图像,如下所示:

[self.imageView setImageWithURL:url];

有什么想法吗?

最佳答案

我遇到了同样的问题。首先你可以使用 AFURLSessionManager 的方法

- (void)setDataTaskWillCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse))block;

同时为所有请求设置缓存 block 。

其次 - 检查响应 header 中的“Cache-Control”字段是否为“public”。例如:

[someAFHTTPSessionManager setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse)
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse*)proposedResponse.response;
NSMutableDictionary *newHeaders = [[resp allHeaderFields] mutableCopy];
if (newHeaders[@"Cache-Control"] == nil) {
newHeaders[@"Cache-Control"] = @"public";
}

NSHTTPURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:@"1.1" headerFields:newHeaders];
NSCachedURLResponse *cachedResponse2 = [[NSCachedURLResponse alloc] initWithResponse:response2
data:[proposedResponse data]
userInfo:[proposedResponse userInfo]
storagePolicy:NSURLCacheStorageAllowed];
return cachedResponse2;
}];

第三:如果图像的大小大于总缓存空间的 5%,则不会被缓存。而且似乎还有一些额外的潜规则。

关于ios - NSURLCache 问题 - 图像从缓存中释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133402/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com