Nimbus for iOS 中 NINetworkImageView 的默认缓存行为是什么
我正在查看 Nimbus 项目中的 NINetworkImageView,并对默认缓存设置感到好奇。一旦我调用 setPathToNetworkImage 并加载图像,该图像是否会进入全局缓存?如果我们创建另一个具有相同 pathToNetworkImage 的 networkImageView ,从而避免网络请求,它是否足够聪明,能够意识到它是相同的图像?
它默认存储在内存还是磁盘中?默认缓存持续时间是多少?
I was looking at the NINetworkImageView in the Nimbus project and was curious about that the default caching settings are. Once I call setPathToNetworkImage and loads an image, does that go in the global cache? Is it smart enough to realize it's the same image if we create another networkImageView with the same pathToNetworkImage and thus avoid a network request?
Does it store it in memory or disk by default? What are the default cache durations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。默认情况下,它进入 Nimbus 的全局内存图像缓存。以下是后台发生的情况:加载图像后,在将图像返回到 UI 线程之前,原始图像将存储在磁盘缓存中[1]。一旦加载线程返回,原始图像就会被设置到 UIImageView 中,并且原始图像也会存储在内存缓存中。
是的。只要它具有所有相同的可配置属性[2],那么它就会立即从内存缓存中加载图像(如果存在)。您可以在此处查看如何生成图像的缓存密钥: https://github.com/jverkoey/nimbus/blob/master/src/networkimage/src/NINetworkImageView.m#L144
[1] 这是因为存储到磁盘是一个阻塞操作,我们不希望用它来阻塞 UI 线程。
[2] 如果您有两个网络图像视图加载相同的 url,但其中一个具有不同的内容模式,则该图像将需要处理两次,因为内存中的缓存键会不同。也就是说,只有图像 URL 用于磁盘缓存键,因此我们最终只会访问网络一次,缓存图像,然后对于第二个网络图像视图,从磁盘加载图像并使用其他内容裁剪它模式。
旁白:这两个缓存属性的文档似乎已被破坏,所以我必须修复这个问题。
Yes. By default it goes into Nimbus' global in-memory image cache. Here's what's going on in the background: once an image loads and before the image is returned to the UI thread the raw image is stored in the disk cache[1]. Once the loading thread returns, the raw image is set to the UIImageView and the raw image is also stored in the in-memory cache.
Yes. As long as it has all of the same configurable properties[2] then it will immediately load the image from the in-memory cache, if it exists. You can see how an image's cache key is generated here: https://github.com/jverkoey/nimbus/blob/master/src/networkimage/src/NINetworkImageView.m#L144
[1] This is because storing to the disk is a blocking operation which we do not want to block the UI thread with.
[2] If you have two network image views loading the same url but one has a different content mode then the image will need to be processed twice because the in-memory cache keys will be different. That being said, only the image URL is used for the disk cache key so we will only end up hitting the network once, caching the image, and then for the second network image view loading the image from disk and cropping it with the other content mode.
Aside: it appears that the documentation for the two cache properties is borked, so I will have to fix this.