How To Clear Image cache or any cache in AFNetworking?

后端 未结 5 1702
半阙折子戏
半阙折子戏 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 
    #import 
    #import 
    
    + (void)clearImageCacheForURL:(NSURL *)url {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
        id  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];
        }
    }
    

提交回复
热议问题