我用 SDWebImage 在我的应用程序中,从网络加载和缓存图像。它的工作方式像魅力一样,但唯一的问题是,每当我退出应用程序时,即使是暂时,图像缓存都会被清除,并且必须再次下载所有图像。当互联网连接缓慢时,这尤其是一个问题。我希望在返回应用程序时保留图像。有人知道如何实现这一目标吗?

有帮助吗?

解决方案

转到sdimagecache.m并寻求方法 - (id)initwithnamespace :( nsstring *)ns 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];

如果需要,请随时发表评论或更改任何内容。我会选择关闭这些代码行。

其他提示

确保您不设置 maxCacheSize[SDImageCache sharedImageCache], ,如果应用程序到达后台时设置,它将清除缓存文件以适合 maxCacheSize.

cleanDisk 方法,您可以在删除文件之前看到一张支票

if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize)
{
// ...
}

因此,如果 maxCacheSize 不是设置,它将无能为力。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top