Pre-Cached Images for the AFNetworking UIImageView Category

When my application loads, I pull out a JSON representation of 99 objects.

Each object has a "image_url" field, which I pass to AFNetworking setImageWithURLRequest .

Loading my images into a tableView, and therefore only the first few cells create queries for their images. This is not until I scroll down that subsequent image requests are made.

As soon as I pulled out the original dataset, I would like to start a background process that exits and loads 95 or so objects that were not initially visible, and cache them in such a way that when setImageWithURLRequest is setImageWithURLRequest , it will already have a cached image.

AFImageCache is private, though, so I'm not sure if this is possible. I know that I can cache NSURLCache, but then I would have two separate isolated caches, and this is also not ideal.

Is my only option not to use the AFNetworking UIImageView category?

These answers make me think like this:

iOS Image Caching Using AFImageCache Doesn't Work
How to configure cache when using setImageWithURL in AFNetworking

+8
ios caching objective-c afnetworking nsurlcache
source share
4 answers

Please, do not do that.

Believe me, when I say that it is almost certainly not necessary.

In fact, it is likely to have the opposite of the desired effect due to the increased loading pressure of images, which are likely to never be considered.

The cache is closed for a very good reason - it just speeds up subsequent requests in scroll views. Just browse through the spreadsheet, upload images as requested, and you should be fine. In any case, you can optimize the size of the downloaded images (ensure the correct image size, intelligently compress).

+24
source share

I would not create a bunch of UIImageView to achieve your goal, it would be a very inefficient approach.

You can add your own method to UIImageView+AFNetworking.h to achieve this ability. I'm losing weight, this will be the best approach. Unverified example:

 + (void) cacheImageWithURL:(NSURL *)url { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request setHTTPShouldHandleCookies:NO]; [request setHTTPShouldUsePipelining:YES]; AFImageRequestOperation *requestOperation = [[[AFImageRequestOperation alloc] initWithRequest:request] autorelease]; [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:request]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) {}]; } 
+5
source share

Here's the solution - create some UIImageView objects (but do not add them as sub-items), and then use the AFNetworking UIImageView category to retrieve the images and fill the internal cache.

Code example:

 NSArray *strings = [NSArray arrayWithObjects: @"http://i.imgur.com/IpQzE.png", @"http://i.imgur.com/sDnLs.jpg", nil]; for (NSString *string in strings) { UIImageView *imageView = [[[UIImageView alloc] init] autorelease]; [imageView setImageWithURL:[NSURL URLWithString:string]]; } 
+3
source share

You might want to consider Fully-Loaded

A simple standalone cache is loading images and saving to a url dependent file name.

Whenever you want to download an image from a URL, try reading it from the hard drive first - if nil was returned, continue downloading the images.

Remember to clear image caches though.

+1
source share

All Articles