首页 > 解决方案 > 在 v5 中将预取图像复制到内存 (SDWebImageCacheMemoryOnly)

问题描述

使用 SDWebImage v4 时,可以设置选项以仅通过以下方式将图像缓存在内存中

SDWebImagePrefetcher.sharedImagePrefetcher.options = SDWebImageCacheMemoryOnly;

这似乎不再是 v5 的选项,因此这个问题。默认情况下,缓存的图像似乎来自光盘。(一些图像延迟出现在屏幕上,我之前通过在内存中预取它们来解决这个问题)。

我在迁移指南中看到 v5 现在拆分了磁盘和内存缓存,但我无法弄清楚如何告诉预取器仅将图像存储在内存中。

标签: iosimagecachingsdwebimage

解决方案


来自图书馆维护者之一的回答,原文可以在https://github.com/SDWebImage/SDWebImage/issues/2698找到

有一个名为 storeCacheType 的上下文选项,值类型为 SDImageCacheType。请参阅有关它的文档。

/**
 A SDImageCacheType raw value which specify the cache type when the image has just been downloaded and will be stored to the cache. Specify `SDImageCacheTypeNone` to disable cache storage; `SDImageCacheTypeDisk` to store in disk cache only; `SDImageCacheTypeMemory` to store in memory only. And `SDImageCacheTypeAll` to store in both memory cache and disk cache.
 If not provide or the value is invalid, we will use `SDImageCacheTypeAll`. (NSNumber)
 */
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextStoreCacheType;

所以。你只需要控制你的预取器,使用 SDImageCacheTypeMemory 枚举来控制这个存储缓存类型。

SDWebImagePrefetcher.sharedImagePrefetcher.context = @{SDWebImageContextStoreCacheType : @(SDImageCacheTypeMemory)};

推荐阅读