How To Clear Image cache or any cache in AFNetworking?

跟風遠走 提交于 2019-12-31 22:20:49

问题


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

回答1:


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




回答2:


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.




回答3:


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



回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/17644197/how-to-clear-image-cache-or-any-cache-in-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!