【问题标题】:React Native Images On Android Never Reload When Uri Doesn't Change当 Uri 不变时,Android 上的 React Native 图像永远不会重新加载
【发布时间】:2022-04-19 16:16:28
【问题描述】:

尽管将新图像上传到 s3 托管 url,但如果 uri 没有更改,<Image> 组件将不会更新图像。据我所知,这只是 Android 的问题,因为对于 iOS,您可以使用 cache: 'reload' 属性。

我已经求助this解决方案:

<Image 
  source={uri: imageUrlFromS3 + '?time=' + new Date(), cache: 'reload'}
  style={width: 100, height: 100}
/>

通过向 url 添加一个无意义的查询,组件识别出 uri 已更改,然后刷新更新的图像。同样,我通过将密钥更改为新的 Date() 来尝试此操作。由于激进的重新渲染,这对于性能来说似乎很糟糕。

好像是this has been a problem for a long time

我见过some answers that require using another package

是否有不需要使用其他包的其他解决方案来解决这个问题?

【问题讨论】:

    标签: android image react-native


    【解决方案1】:

    你只需要这样做:

    source={uri: imageUrlFromS3 + '?' + new Date()}
    

    【讨论】:

    • 不接受这个答案,因为它是我在最初问题中的解决方案的变体。我是否使用“?”或者 "?time=" 不是解决问题的方法。
    【解决方案2】:

    您可以对 unix 时间戳进行子串化,这样它就可以每 N 毫秒重新渲染一次。

    例如,source={uri: imageUrlFromS3 + '?' + Date.now().toString().substring(0,10) + "000" ...

    意味着它将每 1000 毫秒 = 1 秒重新渲染一次。

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 2019-04-02
      • 2012-10-05
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多