【发布时间】:2013-12-03 14:16:40
【问题描述】:
我在我的应用程序中使用SDWebImage 来加载和缓存来自网络的图像。它就像一个魅力,但唯一的问题是,每当我退出我的应用程序时,即使是暂时的,图像缓存都会被清除,并且必须重新下载所有图像。当 Internet 连接速度较慢时,这尤其是一个问题。我希望在返回应用程序时保留图像。有谁知道如何做到这一点?
【问题讨论】:
标签: ios objective-c ios7 sdwebimage image-caching
我在我的应用程序中使用SDWebImage 来加载和缓存来自网络的图像。它就像一个魅力,但唯一的问题是,每当我退出我的应用程序时,即使是暂时的,图像缓存都会被清除,并且必须重新下载所有图像。当 Internet 连接速度较慢时,这尤其是一个问题。我希望在返回应用程序时保留图像。有谁知道如何做到这一点?
【问题讨论】:
标签: ios objective-c ios7 sdwebimage image-caching
转到 SDImageCache.m 并查找方法 - (id)initWithNamespace:(NSString *)ns 在那里你会找到这样的代码:
// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backgroundCleanDisk)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
如果您愿意,请随时评论或更改任何内容。我会选择关闭这些代码行。
【讨论】:
请确保您没有设置[SDImageCache sharedImageCache]的maxCacheSize,如果在应用程序进入后台时设置,它将清除缓存文件以适应maxCacheSize。
在cleanDisk方法中,可以看到删除文件前有个检查
if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize)
{
// ...
}
所以如果没有设置maxCacheSize,它将什么都不做。
【讨论】: