Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Image not showing, How do i fix?

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);
like image 766
Lucas Farleigh Avatar asked Oct 31 '17 12:10

Lucas Farleigh


People also ask

How do I display an image in React Native?

Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.

How do I get the URL of an image in React Native?

So here is the solution to insert an image from an external URL in your application: //MyComponent. js import React from 'react'; import { Image } from 'react-native'; const imageUrl = "https://images.unsplash.com/photo-1526045612212-70caf35c14df"; export class MyComponent extends React.

How do I add a local image in React Native?

To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop's value. to call require with a local image path. And then we set that as the source value to display the image.

How do I add static images to my react native app?

React Native provides a unified way of managing images and other media assets in your Android and iOS apps. To add a static image to your app, place it somewhere in your source code tree and reference it like this: The image name is resolved the same way JS modules are resolved.

Is there a way to show image in react?

your way of show image actually do work in React,I think its the matter of your path of the pic.Do you put all your static files under the public folder,or how do you manage your static pages? This picture is in the same sub-folder of src as my js file.

What version of React Native do I need to upgrade?

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 .

Why is the source attribute named source in React Native?

In React Native, one interesting decision is that the src attribute is named source and doesn't take a string but an object with a uri attribute. On the infrastructure side, the reason is that it allows us to attach metadata to this object.


1 Answers

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.

like image 111
Haseeb A Avatar answered Oct 04 '22 05:10

Haseeb A