1

I'm using the ImageBackground component, imported from react-native, but no image is showing. It's just a white screen in the background

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>

I've tried changing this.backgroundImage to {image: require('../assets/images/default_seal.jpg')}

I tried using the resizeMode property specified by this answer but it didn't work.

Sam
  • 1,765
  • 11
  • 82
  • 176
  • in which file is this piece of code? in your Github repo? – Tom Bombadil Aug 29 '20 at 08:50
  • @MarcusMelodious `components/soundboard.js` sorry – Sam Aug 29 '20 at 08:51
  • Try setting this directly in your ImageBackground component: source={require('../assets/images/default_seal.jpg')} And see it if it displays the image. think the problem is on line 222: this.backgroundImage = {image:{uri: images[Math.floor(Math.random() * 16)]}} Not quire sure why you have it like this in your constructor. Looks like you are passing an array to the uri instead of a string. – Tom Bombadil Aug 29 '20 at 09:05
  • Actually, I just saw the images file in your component. If you are passing an image url. You will have to set it like this: source={{ uri: this.backgroundImage }}. – Tom Bombadil Aug 29 '20 at 09:08

2 Answers2

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

If you render the image property on its own is it definitely correct? e.g.

{this.backgroundImage}

In chrome is the image loading in the network tab, or displaying an error

Do you have any css on the page? If so can you remove it and re-test it. Another alternative:

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

The style:

imgBackground: {
        width: '100%',
        height: '100%',
        flex: 1 
},
Jon Jones
  • 1,014
  • 1
  • 9
  • 17
  • My code is already like this, I just have that style saved inside `styles.container` – Sam Aug 29 '20 at 08:44
0

Edited answer after having a look at the source code. Make your constructor like this:

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

And your ImageBackground component like this:

<ImageBackground source={{uri: this.backgroundImage}}>
Tom Bombadil
  • 3,455
  • 1
  • 13
  • 24