Large Image Caching from Afnetworking

2019-05-10 08:32发布

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条回答
萌系小妹纸
2楼-- · 2019-05-10 09:29

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)];
查看更多
登录 后发表回答