sdwebimage缓存达到最大大小

时间:2015-02-04 09:18:01

标签: ios image caching sdwebimage

我正在使用SDwebimage Framework来缓存图像,因此下载图像数量每天都在增加,因此使用应用程序3-4天后会导致缓存增加,从而导致应用程序性能下降,无论如何都要提高性能

2 个答案:

答案 0 :(得分:0)

看一下这篇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;
                }
            }
        }
    }

答案 1 :(得分:0)

您只需为SDImageCache的共享实例设置全局缓存值。

只需在AppDelegate上调用以下内容:

// limit SDImage cache to 50 MGB

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