Large Image Caching from Afnetworking

回眸只為那壹抹淺笑 提交于 2020-01-13 13:10:39

问题


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]; 

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.

Why small size images are cached & large size images are not cached?? Am I using a wrong way to cache images in AFNetworking?

Can any one give me a solution to cache 2MB images using AFNetworking as well....

Thank you


回答1:


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)];


来源:https://stackoverflow.com/questions/22319018/large-image-caching-from-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!