【问题标题】:sdwebimage cache reaches max sizesdwebimage 缓存达到最大大小
【发布时间】:2015-04-03 18:03:24
【问题描述】:

我正在使用SDwebimage框架缓存图片,所以下载图片的数量每天都在增加,所以在使用应用程序3-4天后,导致缓存增加,导致应用程序性能下降,有没有办法提高性能

【问题讨论】:

    标签: ios image caching sdwebimage


    【解决方案1】:

    看看这个 SO 帖子here

    您可以设置缓存的持续时间,然后将其刷新。此外,您还可以在 cleanDiskWithCompletionBlock 中使用 SDImageCache.m 中的 maxCacheSize 值。

        // If our remaining disk cache exceeds a configured maximum size, perform a second
        // size-based cleanup pass.  We delete the oldest files first.
        if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) {
            // Target half of our maximum cache size for this cleanup pass.
            const NSUInteger desiredCacheSize = self.maxCacheSize / 2;
    
            // Sort the remaining cache files by their last modification time (oldest first).
            NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
                                                            usingComparator:^NSComparisonResult(id obj1, id obj2) {
                                                                return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
                                                            }];
    
            // Delete files until we fall below our desired cache size.
            for (NSURL *fileURL in sortedFiles) {
                if ([_fileManager removeItemAtURL:fileURL error:nil]) {
                    NSDictionary *resourceValues = cacheFiles[fileURL];
                    NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
                    currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
    
                    if (currentCacheSize < desiredCacheSize) {
                        break;
                    }
                }
            }
        }
    

    【讨论】:

    • 非常感谢@ashes to ashes
    • 如何在上述代码块@AshesToAshes 中设置一些最大限制,例如 20 mb
    • @R.Mohan 你找到了限制最大文件大小的方法吗?
    【解决方案2】:

    您可以简单地为共享的 SDImageCache 实例设置一个全局缓存值。

    只需在您的 AppDelegate 上调用以下命令:

    // limit SDImage cache to 50 MGB

    SDImageCache.shared().config.maxCacheSize = 1_000_000 * 50

    【讨论】:

    • 我不是 ios 开发人员,我需要在我的应用委托中配置我的缓存以响应本机。什么是 import 语句以及我会将它放在应用程序委托中的哪个回调?
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 2012-02-08
    • 2012-08-09
    • 2011-02-25
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多