【发布时间】:2015-04-03 18:03:24
【问题描述】:
我正在使用SDwebimage框架缓存图片,所以下载图片的数量每天都在增加,所以在使用应用程序3-4天后,导致缓存增加,导致应用程序性能下降,有没有办法提高性能
【问题讨论】:
标签: ios image caching sdwebimage
我正在使用SDwebimage框架缓存图片,所以下载图片的数量每天都在增加,所以在使用应用程序3-4天后,导致缓存增加,导致应用程序性能下降,有没有办法提高性能
【问题讨论】:
标签: ios image caching sdwebimage
看看这个 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;
}
}
}
}
【讨论】:
您可以简单地为共享的 SDImageCache 实例设置一个全局缓存值。
只需在您的 AppDelegate 上调用以下命令:
// limit SDImage cache to 50 MGB
SDImageCache.shared().config.maxCacheSize = 1_000_000 * 50
【讨论】: