android – React Native:如何使用动态名称导入本地图像

我在本地有大约100个英雄的图像,需要导入到列表视图中.这是我的hero.js

renderRow(hero) {
return (
          <View>
              <View style={styles.container}>
                  <Image
                      source={require('./img/hero/'+hero.img+'.jpg')}
                      style={styles.thumbnail} />
                  <View style={styles.rightContainer}>
                      <Text style={styles.title}>{hero.name}</Text>
                  </View>
              </View>
              <View style={styles.separator} />
          </View>
);

经过一些研究,我知道要求不允许动态名称.我也试过uri,没有错误,但也没有图像.

{{uri: './img/hero/'+hero.img+'.jpg'}}

我的文件夹结构是这样的:

Project
  index.ios.js
  hero.js
  img
    hero

处理此问题的最佳方法是什么,我不想将图像放在网络上.

解决方法:

萨伦斯在这个forum上说了以下内容

We intentionally don’t support dynamically generated image names because it makes it very hard to manage assets over time, just like how you wouldn’t want to do

看起来它可以通过uri完成,但它需要一些工作.你需要在xcode资产中添加你的图像,在res / drawable下添加你的android.

然后,您可以使用< Image source = {{uri:“image”“name”}} />获取您的图片.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...