How to configure the cache when using AFNetworking

2019-03-11 20:46发布

问题:

I'm using setImageWithURL to download some images in my app. Is it possible:

  1. to specify how long this image must be held in cache (eg 1 week)?
  2. to specify how big the the maximum total size of the cache (eg 200 MB)
  3. to see what is in the image cache?
  4. to clear the cache?

The documentation is not really clear on these points..

回答1:

The UIImageView category uses an internal, ephemeral cache for high performance in things like UITableView. For longer-term cache, use the system-level cacheing system--namely Peter Steinberger's fork of SDURLCache, a subclass of NSURLCache.

Set it up with the following code in the application delegate applicationDidFinishLaunching:withOptions::

SDURLCache *URLCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024*2 diskCapacity:1024*1024*20 diskPath:[SDURLCache defaultCachePath]];
[URLCache setIgnoreMemoryOnlyStoragePolicy:YES];
[NSURLCache setSharedURLCache:URLCache];
[URLCache release];