【问题标题】:Image source error in React Native: require expect exactly 1 literal stringReact Native 中的图像源错误:只需要 1 个文字字符串
【发布时间】:2018-07-26 04:45:53
【问题描述】:

您好,我在本地路径中有 iumages 反应原生,我正在尝试提供路径

 <ListItem
    roundAvatar
    avatar={<Avatar
        rounded
        source={require( '../../../public/images/'+props.item.ImageName+'.png')}
        title={props.item.cat_name}
    />}
    onPress={() => props.subscreen(props.item._id)}
    key={props.item._id}
    title={props.item.cat_name}
/>

但是显示错误

在控制台上打印图像路径时,它仅显示字符串。还有什么其他方法或哪里出错了?

【问题讨论】:

    标签: react-native require


    【解决方案1】:

    试试这个:

    require(`../../../public/images/${props.item.ImageName}.png`)
    

    引号的类型不同。

    【讨论】:

      【解决方案2】:

      你不能在 require 中使用变量,只能使用字符串,不能使用变量。 你需要这样做:

      avatar={<Avatar
              rounded
              source={require( '../../../public/images/nameOfPic.png')}
              title={props.item.cat_name}
          />}
      

      如果你想选择一张图片,你可以这样做。

      arrayImgs = [require('../../anyName1.jpg'), require('../../anyName2.jpg')];
      ...
       <ListItem
          roundAvatar
          avatar={<Avatar
              rounded
              source={isTrue?arrayImgs[0]:arrayImgs[1]}
              title={props.item.cat_name}
          />}
          onPress={() => props.subscreen(props.item._id)}
          key={props.item._id}
          title={props.item.cat_name}
      />
      

      编辑:

      您可以将 URI 用于本地图像。

      let imagePath = 'file:///storage/emulated/0/Pictures/'+imageName+'.png'
      ...
      <Image
                    style={{width: '100%', height: 400}}
                    resizeMode='contain'
                    source={{imagePath}}
                  />
      

      这是您 Android 中文件夹的默认路径,请尝试使用“图片”文件夹中的任何图像。

      【讨论】:

      • 我正在使用动态显示 100 行上的 100 个图像而不是行程两个图像。我该如何实现?
      • 所以,如果你有这么多图片,你需要使用'uri',
      • 让 path = 'file://'+pathToResources+'/'+href+'.png'; source={{uri: 路径 }}
      • 你不需要转换,但无论如何你需要下载或放入手机中。
      • 而且你需要知道文件的路径,我举个例子,等等
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 2016-04-26
      相关资源
      最近更新 更多