简体   繁体   English

为什么来自JSON的图像未在react native中的image标签中呈现?

[英]Why image from JSON is not rendered in image tag in react native?

I have a json file 我有一个json文件

{
        "name": "SAM",
        "id": "001",
        "default_image":"../assets/image.jpeg"
}

In my component, I set a state called profile_image with this data called default_image, but when I use it like this, it throws an error. 在我的组件中,我使用名为default_image的数据设置了一个名为profile_image的状态,但是当我这样使用它时,会引发错误。

<Image 
    style={styles.image} 
    source={{uri: this.state.profile_image}}/>

I have given width and height but still it throws an error. 我给出了宽度和高度,但仍然会引发错误。 I have even tried all these possible formats but no success. 我什至尝试了所有这些可能的格式,但没有成功。

<Image 
    style={styles.image} 
    source={{this.state.profile_image}}/>

<Image 
    style={styles.image} 
    source={require(this.state.profile_image)}/>

When using uri to display the image it is displayed using this: 使用uri显示图像时,使用以下方法显示图像:

<Image style={{width:120, height:80,  marginTop:15,alignSelf:'center'}}
source={{uri: this.state.avatarSource}}/>

So, you are not making any syntax error here. 因此,您在这里不会犯任何语法错误。

The problem is with the path of the image you are giving from JSON 问题在于您从JSON提供的图像的路径

Try changing your JSON to this: 尝试将JSON更改为此:

{
        "name": "SAM",
        "id": "001",
        "default_image":"/assets/image.jpeg"
}

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

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