Dheeraj Kaveti
Dheeraj Kaveti

Reputation: 171

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];

Upvotes: 10

Views: 13522

Answers (5)

Kasun Hasitha
Kasun Hasitha

Reputation: 13

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

Upvotes: 1

Anjaneyulu Battula
Anjaneyulu Battula

Reputation: 1960

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];
    }
}

Upvotes: 3

Umair Aamir
Umair Aamir

Reputation: 1644

If you are using AFNetworking using cocoaPods then you can do this by making a category of UIImageView (clearCache)

        - (void)clearImageCacheForURL:(NSURL *)url {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:request];
if (cachedImage) {
    [self clearCached:[[self class] sharedImageCache] Request:request];
}
}

- (void)clearCached:(NSCache *)imageCache Request:(NSURLRequest *)request {
if (request) {
    [imageCache removeObjectForKey:[[request URL] absoluteString]];
}
}

Upvotes: 4

bachonk
bachonk

Reputation: 3954

If you're using AFNetworking, I recently implemented a little workaround to remove the image from the cache.

Look in the file "UIImageView+AFNetworking.h"

Under @interface UIImageView (AFNetworking), add the following:

- (void)clearImageCacheForURL:(NSURL *)url;

And Under @protocol AFImageCache, add:

- (void)clearCachedRequest:(NSURLRequest *)request;

Next, open "UIImageView+AFNetworking.m"

Under @implementation UIImageView (AFNetworking), add the following:

- (void)clearImageCacheForURL:(NSURL *)url {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

    UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:request];
    if (cachedImage) {
        [[[self class] sharedImageCache] clearCachedRequest:request];
    }
}

Almost done! Now just add the following below @implementation AFImageCache:

- (void)clearCachedRequest:(NSURLRequest *)request {
    if (request) {
        [self removeObjectForKey:AFImageCacheKeyFromURLRequest(request)];
    }
}

And you're good to go! Now when you need to clear a particular image url from the cache, just call [imageView clearImageCacheForURL:imgURL];

Upvotes: 16

StuartM
StuartM

Reputation: 6823

https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ

Does AFNetworking have any caching mechanisms built-in?

AFNetworking takes advantage of the caching functionality already provided by NSURLCache and any of its subclasses.

We recommend you try out Pete Steinberger's fork of SDURLCache, which provides disk caching, which is not otherwise implemented by NSURLCache on iOS. You may find it useful to set ignoreMemoryOnlyStoragePolicy to YES for the SDURLCache instance, which will ensure that images will be cached to disk more consistently for offline use (which, as reported in the SDURLCache README, iOS 5 supports, but only for http, so it still remains a good option if you want to support iOS 4 or https)

I would also see this question: How To Disable AFNetworking Cache

Also this: AFNetworking and caching

The moment you activate the NSURLCache you can specify it's maximum size. You can also clear the cache by calling the removeAllCachedResponses or removeCachedResponsesForRequest methods.

Hope it helps.

Upvotes: 6

Related Questions