简体   繁体   English

非常奇怪的反应原生图像问题

[英]Really weird react-native image issue

I have an avatar component... Which returns this...我有一个头像组件......它返回这个......

return (
    <View style={{flex:1, alignItems:'center',marginTop:-10}}>
        <Image source={{uri: this.state.avatar}} />
        <Text style={styles.profileHeader}>
            {this.state.username} 
        </Text>
    </View>
    )

I then am using this avatar by passing the username and avatar as props.然后我通过将用户名和头像作为道具传递来使用这个头像。 This works fine...这工作正常...

   return (
        <View>
          <AvatarHeader   username={d.test_username} avatar={d.test_avatar} />
        </View>     
    );

Now for the weird part.现在是奇怪的部分。 If I do this...如果我这样做...

   return (
        <View>
          <Image source={{uri: d.test_avatar}}  />
          <AvatarHeader   username={d.test_username} avatar={d.test_avatar} />
        </View>     
    );

The Image doesn't display.图片不显示。 The AvatarHeader still displays, but the Image wont. AvatarHeader 仍然显示,但图像不会。 Even though in the AvatarHeader, its literally the exact same image URL being passed.即使在 AvatarHeader 中,它实际上与传递的图像 URL 完全相同。

(And yes, I AM including 'Image' as a require on the main page) (是的,我在主页上包含“图像”作为要求)

I too found out something weird using react-native - realized what is wrong.我也使用 react-native 发现了一些奇怪的东西——意识到出了什么问题。

When you import an image as a file, the image will show no problem (without setting its size) for example:当您将图像作为文件导入时,图像将显示没有问题(无需设置其大小),例如:

<Image source = {require('./img.jpeg')}/>

However if you set the image source from a uri, then the image will not show unless you set the image size on stylesheet.但是,如果您从 uri 设置图像源,则除非您在样式表上设置图像大小,否则图像将不会显示。

<Image source = {{uri: this.state.uri}}/>

The reason is that when the image is imported, the size comes with it.原因是在导入图片的时候,尺寸就自带了。 but when you use uri, the size is undefined.但是当你使用 uri 时,大小是未定义的。

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

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