简体   繁体   English

在 React Native 中重置内部 android 图像缓存

[英]Reset internal android image cache in React Native

I wanted to consulate about a problem using the image library in React Native:我想使用 React Native 中的图像库解决一个问题:

When doing a fetch of the data I get a Json with the urls of the images and they are shown in the screen (the images), if I change the urls of the images in the Json in the new fetch the new Json is downloaded but the images are not update in the view (as if there was an internal cache of android).在获取数据时,我会得到一个带有图像 URL 的 Json,它们显示在屏幕(图像)中,如果我在新的 fetch 中更改 Json 中图像的 URL,则会下载新的 Json,但是图像不会在视图中更新(好像有一个 android 的内部缓存)。

Is it possible to only erase the image cache without any external libraries?是否可以在没有任何外部库的情况下仅擦除图像缓存? Or which library do you recommend to use in this case?或者您建议在这种情况下使用哪个库?

Thank you very much, I hope the answer.非常感谢,希望得到解答。

To my knowledge react-native doesn't support clearing cache out of the box.据我所知,react-native 不支持开箱即用地清除缓存。 What you can do is a quick hack.您可以做的是快速破解。 You can append a random number (like current date in milliseconds) to the image uri that you want to force reload.您可以将一个随机数(如以毫秒为单位的当前日期)附加到要强制重新加载的图像 uri。 Because the uri changed cached image will not be used.因为 uri 更改的缓存图像将不会被使用。

Example示例

export default class App extends Component {
  render() {
    const uri = 'https://picsum.photos/200/300?date=' + (new Date()).getTime();
    return (
      <View style={styles.container}>
        <Image source={{uri}} style={{ width: 200, height: 300}} />
        <Text style={styles.paragraph}>
          Change code in the editor and watch it change on your phone!
          Save to get a shareable url.
        </Text>
      </View>
    );
  }
} 

Try to append random string in url.尝试在 url 中附加随机字符串。 I have fixed the same issue by adding random string.我通过添加随机字符串解决了同样的问题。

'?t=' + Math.round(new Date().getTime() / 1000) '?t=' + Math.round(new Date().getTime() / 1000)

Follow this link for more info 点击此链接了解更多信息

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

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