如何清除AFNetworking中的图像缓存或任何缓存? [英] How To Clear Image cache or any cache in AFNetworking?
问题描述
大家好,有谁能帮我解决如何清除AFNetworking中的缓存。
我曾经使用过旧版本的AFNetworking,并且看到它已经更新了,有谁能帮我解决如何清理AFnetworking的缓存。
以前是这样的
SDImageCache * imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
[imageCache cleanDisk];
如果您使用的是AFNetworking,我最近实现了
在文件 UIImageView + AFNetworking.h中查找
在 @interface UIImageView(AFNetworking)
下,添加以下内容:
-(void )clearImageCacheForURL:(NSURL *)url;
并且在 @protocol AFImageCache
下,添加:
-(void)clearCachedRequest:(NSURLRequest *)request;
下一步,打开 UIImageView + AFNetworking.m
在 @implementation UIImageView(AFNetworking)下
,添加以下内容:
-(void)clearImageCacheForURL:(NSURL *)url {
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[为HTTPHeaderField:@ Accept请求addValue:@ image / *];
UIImage * cachedImage = [[[[self class] sharedImageCache] cachedImageForRequest:request];
if(cachedImage){
[[[[self class] sharedImageCache] clearCachedRequest:request];
}
}
几乎完成了!现在,只需在下面的 @implementation AFImageCache
中添加以下内容:
-(void )clearCachedRequest:(NSURLRequest *)request {
if(request){
[self removeObjectForKey:AFImageCacheKeyFromURLRequest(request)];
}
}
您很高兴!现在,当您需要从缓存中清除特定的图像URL时,只需调用 [imageView clearImageCacheForURL:imgURL];
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];
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];
这篇关于如何清除AFNetworking中的图像缓存或任何缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!