2016-11-09 117 views
3

我试图清除缓存位,应用程序的大小不会变得明显变小。清除缓存后图像不会从磁盘上删除AFNetworking

我的应用程序大小约为30MB,缓存图像为45MB。

这是我试图清除缓存:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[[AFImageDownloader defaultURLCache] removeAllCachedResponses]; 
AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache; 
[imageCache removeAllImages];    
  • 在我的iPhone 6 iOS10应用程序的大小并没有变小。
  • 模拟器上的应用程序文件内com.alamofire.imagedownloader/fsCachedData所有文件仍然存在

这是我如何使用AFNetworking(3.1.0)下载图片:

#import "AFImageDownloader.h" 

[photoImageView setImageWithURLRequest:[NSURLRequest requestWithURL:thumb] placeholderImage:myCachedImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
       [self setPhotoImage:image]; 
      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
       [self setupPlaceholder]; 
      }]; 
+0

我发现缓存并不总是在模拟器的工作,尝试真正的设备。 –

回答

1

见如下:

  1. AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache;imageCache存储在内存中,做无用[imageCache removeAllImages];
  2. 您只需要做[[AFImageDownloader defaultURLCache] removeAllCachedResponses];。这是苹果提供的唯一清除URLCache的API。
  3. 当您拨打removeAllCachedResponses时,您会发现fsCachedData未被删除。这是因为苹果有自己的机制来清除磁盘数据。也许它会在我搜索时每7天清除一次磁盘数据,但我无法确定。
  4. 我认为看到别人如何使用fsCachedData很有意思。 Should I clear the fsCachedData folder myself?
  5. 我希望它能帮助你。
1

我们也在我们的一个应用中看到了这一点。根据这个http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/,它的一个iOS 8 bug将在今天的iOS 8.1更新中得到纠正。

所以要回答你的问题:不,你不应该自己删除缓存,并有一定的运气苹果的修复确实会清理旧数据。如果由于某种原因,您应该可以自己删除它,而不会有任何问题。

我已经验证了iOS 8.1确实可以解决问题。用户必须在更新后启动应用程序,然后才能清除缓存。

+0

你为什么在谈论iOS 8,并链接到2014年的博客帖子?我们现在是2016年,大多数人都在使用iOS 10.我遇到与原始海报相同的问题,但我怀疑这与iOS 8中的错误有关(iOS8.1中已修复此错误)。 – knutigro

0

此代码为AFNetworking 3.1:

 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 

     AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance]; 
     NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache; 
     [urlCache removeAllCachedResponses]; 

     [imageDownloader.imageCache removeAllImages];