How To Clear Image cache or any cache in AFNetworking?

后端 未结 5 1546
半阙折子戏
半阙折子戏 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: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]];
    }
    }
    

提交回复
热议问题