Image.prefetch不会自动从缓存中加载[React-Native/Expo]
问题描述:
我想了解如何缓存图片url,以便它不需要重新下载。 我已经采取一看:https://docs.expo.io/versions/v19.0.0/guides/preloading-and-caching-assets.html 并一直在使用Image.prefetch像这样:Image.prefetch不会自动从缓存中加载[React-Native/Expo]
const prefetchedImages = images.map(url => {
console.log('url', url) //this is correctly logging the url
return Image.prefetch(url)
});
Promise.all(prefetchedImages)
.then(() => {
this.setState({loaded:true})
})
这最终并设置状态为真。然后,我在不同的组件中呈现图像,但是我确保预取的组件不会卸载。我加载URL像这样:
<Image source={{uri: myImageUrl}} style={{width:100, height:100}} />
当我载入图片到我的网格视图中,只有本地的图像立即显示,并与URL中的有白了一会儿渲染之前。在iOS上使用缓存:'force-cache'时,图像实际上是从缓存中加载的,并且没有延迟。如果我使用预取,我认为我不需要那样做。
我在这里错过了什么吗?我以为我可以像往常一样调用我的图像源,系统会知道如何抓取该网址的缓存图像。
答
我不认为预取就像从缓存中加载图像一样简单,只是因为它在缓存中,如果这是有道理的。
看看这个帖子https://github.com/facebook/react-native/issues/2314和这个问题React native Image prefetch。
如果要保存图像并使它们始终可用,请查看https://www.npmjs.com/package/react-native-preload-images。
你可以尝试实现这个解决方案https://medium.com/one-more-thing-studio/caching-your-images-on-react-native-with-expo-7bff361dbd54 – Jeremie
你不能依靠预取在大多数情况下。我在https://medium.com/@wcandillon/5-things-to-know-about-images-react-native-69be41d2a9ee上撰写了关于该主题的中型故事 – wcandillon