通过Afnetworking进行大图像缓存 [英] Large Image Caching from Afnetworking
问题描述
我正在使用AFNetworking将一些图像从互联网下载到我的应用程序中。我正在使用此代码下载这些图像,
I'm using AFNetworking to download some images from the internet to my app. I'm using this code to download those images,
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_linkString[indexPath.item]]]];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
_imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
我注意到所有响应图像都通过此方法自动缓存到磁盘。我也希望在我的应用中使用该选项。所有缓存的图像约为150kb。但是,当我下载大约2MB大小的图像时,这些图像不会自动缓存到磁盘。
I noticed that all the response images are cached to the disk automatically from this method. I want that option too in my app. All the cached images are about 150kb size. But when I download an image about 2MB size, those images are not cached automatically to disk.
为什么要缓存小尺寸的图片&大尺寸图像不缓存?我是否使用错误的方法在AFNetworking中缓存图像?
Why small size images are cached & large size images are not cached?? Am I using a wrong way to cache images in AFNetworking?
有人可以给我一种使用AFNetworking缓存2MB图像的解决方案吗....
Can any one give me a solution to cache 2MB images using AFNetworking as well....
谢谢
推荐答案
问题不在于AFNetworking,而在于NSURLCache。默认情况下,NSURLCache不会缓存大于缓存大小的10%(不确定确切百分比)的文件。
The problem is not with AFNetworking but with NSURLCache. By default NSURLCache will not cache files bigger then 10% (not sure what the exact percentage is) of the cache size.
但是增加缓存大小会有所帮助:
But increase the cache size will help:
[[NSURLCache sharedURLCache] setMemoryCapacity:(20*1024*1024)];
[[NSURLCache sharedURLCache] setDiskCapacity:(200*1024*1024)];
这篇关于通过Afnetworking进行大图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!