Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Clear Image cache or any cache in AFNetworking?

Hi All can any one help me out how to clear the cache in AFNetworking.

I used to have old version of AFNetworking and i see that it has been updated, Can any body help me out how to clean the cache for AFnetworking.

Previously it was something like this

SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
[imageCache cleanDisk];
like image 345
Dheeraj Kaveti Avatar asked Jul 14 '13 21:07

Dheeraj Kaveti


2 Answers

AFImageDownloader *imageDownloader = [AFImageDownloader   defaultInstance];

NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;

[urlCache removeAllCachedResponses];

[imageDownloader.imageCache removeImageWithIdentifier:url];

this will help to remove image in cash. url is the image url that needs to remove

like image 159
Kasun Hasitha Avatar answered Sep 24 '22 18:09

Kasun Hasitha


If you are using AFNetworking3.0 through cocoapods, then use below code by creating category for UIImageView.

#import <AFNetworking/UIImageView+AFNetworking.h>
#import <AFNetworking/AFImageDownloader.h>
#import <AFNetworking/AFAutoPurgingImageCache.h>

+ (void)clearImageCacheForURL:(NSURL *)url {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    id <AFImageRequestCache> imageCache = [[self class] sharedImageDownloader].imageCache;
    UIImage *cachedImage = [imageCache imageforRequest:request withAdditionalIdentifier:nil];
    if (cachedImage) {
        [self clearCached:imageCache Request:request];
    }
}

+ (void)clearCached:(AFAutoPurgingImageCache *)imageCache Request:(NSURLRequest *)request {
    if (request) {
        [imageCache removeImageforRequest:request withAdditionalIdentifier:nil];
    }
}
like image 40
Anjaneyulu Battula Avatar answered Sep 24 '22 18:09

Anjaneyulu Battula