使用AFNetworking的setImageWithURL时如何配置缓存 [英] How to configure the cache when using AFNetworking's setImageWithURL
问题描述
我正在使用 setImageWithURL
在我的应用中下载一些图片。是否有可能:
I'm using setImageWithURL
to download some images in my app. Is it possible:
- 指定此图像必须在缓存中保留多长时间(例如1周)?
- 指定缓存的最大总大小有多大(例如200
MB) - 以查看图像缓存中的内容?
- 清除缓存?
- to specify how long this image must be held in cache (eg 1 week)?
- to specify how big the the maximum total size of the cache (eg 200 MB)
- to see what is in the image cache?
- to clear the cache?
文档在这些方面并不是很清楚..
The documentation is not really clear on these points..
推荐答案
UIImageView
类别使用内部短暂缓存在 UITableView
等内容中实现高性能。对于长期缓存,请使用系统级缓存系统 - 即Peter Steinberger的 SDURLCache
的分支, NSURLCache $ c $的子类c>。
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
.
使用应用程序委托中的以下代码进行设置 applicationDidFinishLaunching:withOptions:
:
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];
这篇关于使用AFNetworking的setImageWithURL时如何配置缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!