How To Clear Image cache or any cache in AFNetworking?

后端 未结 5 1544
半阙折子戏
半阙折子戏 2021-02-06 00:30

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 m

相关标签:
5条回答
  • 2021-02-06 00:37

    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];
        }
    }
    
    0 讨论(0)
  • 2021-02-06 00:45

    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]];
    }
    }
    
    0 讨论(0)
  • 2021-02-06 00:50

    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.

    0 讨论(0)
  • 2021-02-06 00:51
    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

    0 讨论(0)
  • 2021-02-06 00:58

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

    0 讨论(0)
提交回复
热议问题