1

我正在使用从 导入的 ImageBackground 组件,react-native但没有显示图像。只是背景中的白屏

constructor(props){
    super(props)
    this.backgroundImage = {image: {uri: "https://i.myImage.com"}}
}

......

<ImageBackground style={styles.container} resizeMode="stretch" source={this.backgroundImage} onError={this.onError.bind(this)}>
    <AdUnit style={styles.bannerAd} />
    <Board  />
</ImageBackground>

我试过this.backgroundImage改成{image: require('../assets/images/default_seal.jpg')}

我尝试使用此答案指定的 resizeMode 属性,但它不起作用。

4

2 回答 2

0
<ImageBackground
  source={yourSourceFile}
  style={{width: '100%', height: '100%'}}
> 
    <....yourContent...>
</ImageBackground>

如果您自己渲染图像属性,它绝对正确吗?例如

{this.backgroundImage}

在 chrome 中是在网络选项卡中加载的图像,或显示错误

页面上有css吗?如果是这样,您可以将其删除并重新测试。另一种选择:

<ImageBackground style={ styles.imgBackground } 
                 resizeMode='cover' 
                 source={require('./Your/Path.png')}>
</ImageBackground>

样式:

imgBackground: {
        width: '100%',
        height: '100%',
        flex: 1 
},
于 2020-08-29T08:43:19.480 回答
0

查看源代码后编辑答案。使您的构造函数像这样:

 constructor(props){
        super(props)
        this.backgroundImage = images[Math.floor(Math.random() * 16)]

你的 ImageBackground 组件是这样的:

<ImageBackground source={{uri: this.backgroundImage}}>
于 2020-08-29T08:44:52.293 回答