Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

How can I clean images cache and change cache policy? #109

Closed
Ricardo1980 opened this Issue Nov 11, 2011 · 16 comments

Comments

Projects
None yet

Hello!

Is it possible to change cache policy?
Can be cleared cached images?

Thanks.

Contributor

mattt commented Nov 11, 2011

Howdy howdy,

The cache policy of image requests is managed by NSURLRequest. Just define the appropriate NSURLRequestCachePolicy when constructing one with requestWithURL:cachePolicy:timeoutInterval:, and pass that request into `AFImageRequestOperation".

The image cache is managed by AFImageCache. To manually clear the cache, do [[AFImageCache sharedImageCache] removeAllObjects];

@mattt mattt closed this Nov 11, 2011

Thanks a lot!

Enviado desde mi Sinclair ZX Spectrum 128.

El 11/11/2011, a las 17:20, Mattt Thompson reply@reply.github.com escribió:

Howdy howdy,

The cache policy of image requests is managed by NSURLRequest. Just define the appropriate NSURLRequestCachePolicy when constructing one with requestWithURL:cachePolicy:timeoutInterval:, and pass that request into `AFImageRequestOperation".

The image cache is managed by AFImageCache. To manually clear the cache, do [[AFImageCache sharedImageCache] removeAllObjects];


Reply to this email directly or view it on GitHub:
https://github.com/gowalla/AFNetworking/issues/109#issuecomment-2709846

@mattt hi how do you do it now since a lot of the code has changed.

Since + (AFImageCache *)af_sharedImageCache isn't public?

@interface AFImageCache : NSCache
- (UIImage *)cachedImageForRequest:(NSURLRequest *)request;
- (void)cacheImage:(UIImage *)image
        forRequest:(NSURLRequest *)request;
@end

Thanks in advance.

vhesener commented Jul 9, 2012

@mattt I was under the impression that if you received memory warnings in iOS, you should clear any in memory cache's that you have. A second warning will shut down your app so it's your chance to save your app from the home screen of death (HSOD) :)

Correct me if I'm wrong, but I'm using this to implement a potentially large scrollview with web images. Therefore, the cache will grow and grow, right? And won't that be the same as just keeping all the images in memory? Maybe I'm missing something obvious.

Contributor

mattt commented Jul 9, 2012

AFImageCache is a subclass of NSCache, which automatically evicts objects according to the amount of memory available. You can safely use UIImageView loading and cacheing transparently in AFNetworking without manually touching anything.

This is the reason why it's private: I don't want users to think that they have to manage this themselves.

vhesener commented Jul 9, 2012

touché and agree.

spulver commented Sep 3, 2012

How can I force to reload a single image (already cached) without clearing the full cache?

spulver commented Sep 7, 2012

@xissburg could you please be a little more explicit for an old man |-(

xissburg commented Sep 7, 2012

@spulver I mean, I also would like to know how to force a reload. I am communicating with an web service where I update the user avatar image and it does not change the URL after the update :/

lieven commented Dec 12, 2012

NSCache should do this automatically, but when you don't set totalCostLimit on the cache and cost on each cached image, it doesn't seem like memory warnings trigger a cleanup.

You can see this by using Instruments (allocations), and triggering a memory warning in the simulator.

@spulver the best way is to replace the image using the cacheImage:forRequest: method at the bottom of UIIMageView+AFNetworking. Unfortunately, you'll have to take the same route as @vhesener to expose the method. You can pass in a basic NSURLRequest without worrying about fully configuring it like you would for setImageWithURLReqeust.... Or if you really do want to reload the image, you can use NSCache-(void)removeObjectForKey in a similar manner to clearAFImageCache.

I see this is quite old but I'm sure there are people search for a solution to this common problem. You can override the cached image without exposing private classes and methods by using an explicit NSURLRequest with the correct caching policy to set the image:

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
 [imageView setImageWithURLRequest:request
                               placeholderImage:placeholdeImage
                                            success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                               dispatch_async(dispatch_get_main_queue(), ^{
                                                   imageView.image = image;
                                               });

                                           } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                               NSLog(@"failed loading image: %@", error);
                                           }];

This will both get the actual image from the server and update the cache for future usage…

This is old but quite useful ,I am very glad to be here to learn something. Thank you all.

teffi commented Sep 12, 2014

@ekurutepe
Thanks for the solution.
Seems like the fix by Matt #194 is not working well now. And yours did the job.

@ekurutepe
Thanks for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment