Alex L Alex L - 10 months ago 148
iOS Question

Images are not removed from disk after clearing cache AFNetworking

I tried to clear cache bit the size of the app does not become significantly smaller.

My app size is about 30MB plus 45MB for cached images.

This is how I attempted to clear cache:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[AFImageDownloader defaultURLCache] removeAllCachedResponses];
AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache;
[imageCache removeAllImages];

  • On my iPhone 6 iOS10 the size of the app did not get smaller.

  • On Simulator inside app files
    all the files are still there

This is how I download images using 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];

Answer Source

See follows:

  1. AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache; The imageCache is stored in the memory, it is useless to do [imageCache removeAllImages];
  2. You just need to do [[AFImageDownloader defaultURLCache] removeAllCachedResponses];. It is the only API that apple offers to clear the URLCache.
  3. You find the fsCachedData is not removed when you call removeAllCachedResponses. It is because apple has its own mechanism to clear the disk data. Maybe it will clear the disk data in every 7 days as i searched, but i can not make sure.
  4. I think it is interesting to see how the others use fsCachedData. Should I clear the fsCachedData folder myself?
  5. I hope it will help u.