Pre-cache Images for AFNetworking's UIImageVie

2019-04-24 07:01发布

When my app loads, I pull down a JSON representation of 99 objects.

Each object has an 'image_url' field, which I pass to AFNetworking's setImageWithURLRequest.

My images load in a tableView, and consequently, only the first several cells make requests for their images. It's not until I scroll down that subsequent image requests are made.

Once I pull down the initial dataset, I'd like to be able to kick off a background process that goes out and downloads the 95 or so objects that aren't initially visible, and cache them in such a way that when setImageWithURLRequest is called, it'll already have a cached image to pull from.

AFImageCache is private though, so I'm not sure if this is possible. I know I could cache with NSURLCache, but then I'd have two separate, isolated caches, and that's not ideal either.

Is my only option to not use AFNetworking's UIImageView category?

These answers make me think so:

iOS Caching images with AFImageCache doesn't seem to work
How to configure the cache when using AFNetworking's setImageWithURL

4条回答
欢心
2楼-- · 2019-04-24 07:34

You probably want to consider Fully-Loaded

A simple self maintained cache is to download the images and save to a url dependent filename.

Whenever you want to load a image from a url, try read from harddisk first - if nil was returned, go ahead with your image downloader.

Do remember to clear your image caches though.

查看更多
Ridiculous、
3楼-- · 2019-04-24 07:49

Here's a solution - create some UIImageView objects (but don't add them as subviews) and then use AFNetworking's UIImageView category to fetch the images and populate its internal cache.

Sample Code:

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]];
}
查看更多
Fickle 薄情
4楼-- · 2019-04-24 07:55

Please, please don't do this.

Trust me when I say that this almost certainly unnecessary.

In fact, it will likely have the opposite of the desired effect, due to the increased pressure of downloading images that will probably never be viewed.

The cache is private for a very good reason--it's just there to speed up subsequent requests on scroll views. Just have the table view download the images as requested, and you should be just fine. If anything, you can optimize the size of the images that you are downloading (ensure correct image dimensions; compress intelligently).

查看更多
Explosion°爆炸
5楼-- · 2019-04-24 07:55

I would NOT create a bunch of UIImageView's to achieve your purpose, that would be a really inefficient approach.

You could add your own method to UIImageView+AFNetworking.h to achieve this ability. I thin this would be the best approach. An untested example would be:

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