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

标签 android react-native

我想使用 image library 解决问题在 React Native 中:

当获取数据时,我得到一个带有图像 url 的 Json,它们显示在屏幕(图像)中,如果我在新的 Json 中更改图像的 url,则获取新的 Json已下载,但图像未在 View 中更新(好像有 android 的内部缓存)。

是否可以在没有任何外部库的情况下只删除图像缓存? 或者您建议在这种情况下使用哪个库?

非常感谢,希望得到解答。

最佳答案

据我所知,react-native 不支持开箱即用的清除缓存。您可以做的是快速破解。您可以将一个随机数(例如以毫秒为单位的当前日期)附加到要强制重新加载的图像 uri。因为 uri 改变了缓存的图像将不会被使用。

示例

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>
    );
  }
} 

关于android - 在 React Native 中重置内部 android 图像缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49603434/

相关文章:

javascript - 无法在 react JS 中向下滚动屏幕

react-native - React Native 开发服务器返回响应错误代码 :500

android - 更新 Glass 时间线卡的更好解决方案

php - 通过php web服务将mongodb中的数据解析为android应用程序

android - 更新 ExpandableListView 中的子项

java - 获取滑动面板布局事件

android - 在 fragment 和 Activity 之间更新 ViewModel

react-native - react 导航 : Prevent multiple instances to navigate to same screen

javascript - React Native - 显示键盘并渲染其值而不使用输入

javascript - 在react-native功能组件中哪里实现初始化代码