ios - 使用 AFNetworking 进行图像缓存

标签 ios objective-c caching afnetworking-2

AFNetworking 的缓存机制对我不起作用,我有一张 500ko 的大尺寸图像,我想缓存它。但是每次我关闭应用程序并再次打开它时,我都必须等待 20 秒才能加载图像,因此缓存系统无法正常工作,代码如下:

我试过了:

[imageView setImageWithURL:[NSURL URLWithString:imageURL]
      placeholderImage:[UIImage imageNamed:@"placeholder"]];

然后尝试:

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]
                                          cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                      timeoutInterval:60];

[imageView setImageWithURLRequest:imageRequest
             placeholderImage:[UIImage imageNamed:@"placeholder"]
                      success:nil
                      failure:nil];

最佳答案

NSURLCache 默认情况下不会写入磁盘,因此我们需要在应用委托(delegate)中声明一个共享的 NSURLCache :

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                          diskCapacity:100 * 1024 * 1024
                                          diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

Reference

关于ios - 使用 AFNetworking 进行图像缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31114846/

相关文章:

android - 如何查看 Android 应用程序特定缓存?

Mysql使用系统文件缓存,而不是直接使用RAM

ios - NSURLSession 鉴权和后台下载文件

ios - 在设备方向更改时在 UiView 中居中 UIActivityIndi​​catorView

iphone - 当 tableViewController 位于 navigationController 内时,无法在方向更改之前获取 reloadData 来更新表

ios - UITableView 和 Autolayout 性能不佳

c++ - 对 std::atomic 变量的更改(读/写)如何跨线程传播

ios - 如何在程序运行时禁用(停用)按钮?

android - 用于第 3 方/内置应用程序的 iPhone 自动化

ios - 如何将包含\u转义字符的JSON解码为UTF8 NSString?