简体   繁体   English

Glide:在 memory 缓存中预加载图像(有或没有磁盘缓存)

[英]Glide: Preload images in memory cache (with or without disk cache)

I am using我在用

glide.load(url)
     .diskCacheStrategy(DiskCacheStrategy.ALL)    
     .preload()

to preload images.预加载图像。

However, I need them to be in memory and not just on disk, so it's loaded in ImageView more quickly, the way it does when I revisit the images after loading them in ImageView once.但是,我需要它们在 memory 中,而不仅仅是在磁盘上,所以它在ImageView中加载得更快,就像我在ImageView中加载图像一次后重新访问图像时的方式一样。

I have also tried我也试过

  glide.load(url)
       .diskCacheStrategy(DiskCacheStrategy.ALL)
       .into(PreloadTarget.obtain(glide, PreloadTarget.SIZE_ORIGINAL, PreloadTarget.SIZE_ORIGINAL))

without much luck.没有太多运气。

PS: I have visited this question and others, answers are outdated hence this question. PS:我已经访问过这个问题和其他问题,答案已经过时,因此这个问题。

Referring to this article you can cache the image in the memory then use参考这篇文章你可以将图像缓存在内存中然后使用

onlyRetrieveFromCache( true )

to load the image only from memory仅从内存加载图像

In order to get cached resource from the memory cache, loaded objects should be exactly equal.为了从 memory 缓存中获取缓存资源,加载的对象应该完全相等。 Read in docs here . 在此处阅读文档 To verify if your preloaded object and object loaded to ImageView are the same enable Glide logs for the Engine class.要验证您预加载的 object 和加载到 ImageView 的 object 是否相同,请为引擎class 启用 Glide 日志。

adb shell setprop log.tag.Engine VERBOSE adb shell setprop log.tag.Engine 详细

Then compare the EngineKey objects that you see in logs for preload and for actual load to ImageView. They should not only have the same url and signature, but also transformations and options.然后将您在日志中看到的预加载和实际加载的EngineKey对象与 ImageView 进行比较。它们不仅应该具有相同的 url 和签名,而且还应该具有相同的转换和选项。

In case you load into ImageView, to not add any transformation based on ImageView params, use the如果你加载到 ImageView,不添加任何基于 ImageView 参数的转换,使用

RequestOptions().dontTransform() RequestOptions().dontTransform()

for the load to ImageView request.用于加载到 ImageView 请求。

Preload with Glide v4.6.1使用 Glide v4.6.1 预加载

RequestOptions requestOptions = RequestOptions
        .diskCacheStrategy(DiskCacheStrategy.ALL);

Glide.with(appContext)
        .asBitmap()
        .load(model)
        .apply(requestOptions)
        .submit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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