简体   繁体   中英

Replicate Prefetching images into memory (SDWebImageCacheMemoryOnly) in v5

While using SDWebImage v4 it was possible to set options to cache images in memory only via

SDWebImagePrefetcher.sharedImagePrefetcher.options = SDWebImageCacheMemoryOnly;

This no longer seems to be an option with v5, hence this question. By default it seems that cached images are coming from disc. (Some images appear on screen with delay, which I previously solved by prefetching them in memory).

I saw in migration guide that v5 now splits disc and memory cache, but I am not able to figure out how to tell prefetcher to store images in memory only.

Answer from one of the library maintainers, original can be found at https://github.com/SDWebImage/SDWebImage/issues/2698

There is a context option called storeCacheType, the value type is SDImageCacheType. See the documentation about it.

/**
 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;

So. You just need to control your prefetcher, to use the SDImageCacheTypeMemory enum for this store cache type control.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM