29

I was wondering why my image is not showing. What I want is for my image to be in the background with my two buttons on the bottom, over the image. I am using react native, with the IDE 'Deco' for apps. Right now there is no image showing at all:

import React, { Component } from 'react';
import { Button,Alert, TouchableOpacity,Image, Dimensions } from 'react-native'

import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} from 'react-native';

class Project extends Component {
  render() {
    return (
      <View style={{backgroundColor: '#375D81', flex: 1}}>
        <Image source={{uri: 'https://upload.wikimedia.org/wikipedia/commons/f/f0/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006_edit_1.jpg'}}/>
         <View style = {styles.container}>
           <TouchableOpacity style = {styles.buttonText1} onPress={() => { Alert.alert('You tapped the button!')}}>
             <Text style={styles.text}> 
              Button 1
             </Text>
           </TouchableOpacity>
           
           <TouchableOpacity style = {styles.buttonText2} onPress={() => { Alert.alert('You tapped the button!')}}>
             <Text style= {styles.text}>
              Button 2
             </Text>
           </TouchableOpacity>
           
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
 main: {
   backgroundColor: 'blue'
 },
 text: {
 alignItems : 'center'
               
 },
 container: {
  alignItems: 'center',
  flex: 1,
 },
  buttonText1: {
      borderWidth: 1,
      padding: 25,      
      borderColor: 'black',
      backgroundColor: '#C4D7ED',
      alignItems: 'center',
      position: 'absolute',
      bottom: 0,
      width: Dimensions.get('window').width / 2,
      height: Dimensions.get('window').height / 8,
      left: 0,
   },
    buttonText2: {
      borderWidth: 1,
      padding: 25,      
      borderColor: 'black',
      backgroundColor: '#C4D7ED',
      alignItems: 'center',
      position: 'absolute',
      bottom: 0,
      width: Dimensions.get('window').width / 2,
      height: Dimensions.get('window').height / 8,
      right: 0,
   }
});

AppRegistry.registerComponent('Project', () => Project);
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
Lucas Smith
  • 698
  • 2
  • 9
  • 17

10 Answers10

52

If this is happening in ios 14 or xcode 12, You have to upgrade react native to 0.63.2 There is one bug in react-native versions bellow that.

To patch the issue for RN versions less than 0.63.2 add this to the end of your Podfile .

pre_install do |installer|
  puts("Image fix for ios14: remove this when upgradeing to >= 0.63.3")
  find = "_currentFrame.CGImage;"
  replace = "_currentFrame.CGImage ;} else { [super displayLayer:layer];"
  op = `sed -ie "s/#{find}/#{replace}/" ../node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m`
  puts("Image fix for ios14 done")
end

Then run pod install

Or if you want a package based solution you can use https://www.npmjs.com/package/react-native-fix-image . If you are using this it is recommended to add it as a npm postinstall script.

no need to edit node_modules.

Haseeb A
  • 5,356
  • 1
  • 30
  • 34
49

make some width and height for the image..

 <Image
   style={{width: 50, height: 50}}
   source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
   resizeMode={'cover'} // cover or contain its upto you view look
   />

Try to undesrtand this...

Here i mentioned width and height... you can make it '100%' its upto you...

Idan
  • 3,604
  • 1
  • 28
  • 33
8

After updating IOS 14.x it is an issue being generated that image is not being shown. Here is some info against that.

It can display the image after adding [super displayLayer:layer]; if _currentFrame is nil

if I understand correctly, _currentFrame should be for animated image, so if it is still image, we can use the UIImage implementation to handle image rendering, not sure if it is a correct fix.

node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m

if (_currentFrame) {
    layer.contentsScale = self.animatedImageScale;
    layer.contents = (__bridge id)_currentFrame.CGImage;
  } else {
    [super displayLayer:layer];
  }
Soban Arshad
  • 1,295
  • 19
  • 15
4

You need to specify the dimensions:

  <Image source={{uri: 'https://upload.wikimedia.org/wikipedia/commons/f/f0/Everest_North_Face_toward_Base_Camp_Tibet_Luca_Galuzzi_2006_edit_1.jpg'}} style={{ resizeMode: 'cover', width: '100%', height: '100%' }}/>
SiHa
  • 7,830
  • 13
  • 34
  • 43
TheCodeTalker
  • 685
  • 6
  • 14
0

This issue is related to react-native version and it can display the image after adding [super displayLayer:layer]; or upgrade react-native version.

go to node_module/react-native/Libraries/Image/RCTUIImageViewAnimated.m and add [super displayLayer:layer];

- (void)displayLayer:(CALayer *)layer
{
  if (_currentFrame) {
    layer.contentsScale = self.animatedImageScale;
    layer.contents = (__bridge id)_currentFrame.CGImage;
  }else {
    [super displayLayer:layer];
  }
}
Manoj Alwis
  • 1,337
  • 11
  • 24
0

I've faced this issue and this is what I've noticed.

Applying width and height directly does not work properly.

<Image src="https://picsum.photos/800/400" width={400} height={200}/>

Instead apply them inside style object. And this will work.

<Image src="https://picsum.photos/800/400" style={{width: 400, height: 200}}>
djsdev
  • 76
  • 2
  • 4
0
import {View, Image} from 'react-native'; 
...
...
<View style={{width: '100%', marginTop: 10'}}>     
<Image style={{width: 50, height: 50}}source={{uri: "link"}}/>
</View>
  • 3
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 17 '22 at 00:26
0

in my case all the images and icon not show in IOS project i have an old react native(version is 0.62.2) project

so i added below code in podfile in last

end
 pre_install do |installer|
 find = "_currentFrame.CGImage;"
 replace = "_currentFrame.CGImage ;} else { [super displayLayer:layer];"
 op = `sed -ie "s/#{find}/#{replace}/" ../node_modules/react- 
 native/Libraries/Image/RCTUIImageViewAnimated.m`
end

enter image description here

Deepak Singh
  • 749
  • 4
  • 16
0

I fixed mine by replacing the 'width' and 'height' section from the styling with;

resizeMode: "cover"

see image here

Then you can adjust the size after you see image has displayed

0

I think you need to use the require function.

Source= {require('imageaddress')}

  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney Aug 20 '23 at 00:13