3

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

Logan
  • 52,262
  • 20
  • 99
  • 128
DilumN
  • 2,889
  • 6
  • 30
  • 44

1 Answers1

7

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)];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • What is the default cache size for a app in the Disk? & where should I use this [[NSURLCache sharedURLCache] setDiskCapacity:(200*1024*1024)]; code? in AppDelegate?? I'm new to URL Caching. Sorry for that. – DilumN Mar 11 '14 at 07:52
  • Not sure what the default size. I've placed this code in my AppDelegate `application:didFinishLaunchingWithOptions:` – rckoenes Mar 11 '14 at 07:54