Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't show Image in React Native

I'm using react-native 0.28.0

I'm trying to show an image on iPhone simulator according to this tutorial: Introduction to React Native: Building iOS Apps with JavaScript | Appcoda

var styles = StyleSheet.create({ image: {     width: 107,     height: 165,     padding: 10   } }  var imageURI = 'http://books.google.com/books/content?id=PCDengEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api' 

Then in the render() function, I add this:

<Image style={styles.image} source={{uri:imageURI}} /> 

The space allocated for the image is there, but the image is not shown.

like image 588
Fredric Sanjaya Avatar asked Jun 30 '16 05:06

Fredric Sanjaya


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 display an image in uri in React Native?

To load images in react native from URL you must specify the image width, and height. React native cannot determine the width and height from downloaded images. After specifying width, and height we can specify the URL to the image source prop. Inside this prop we will specify a uri which holds our image URL.

How do I display images in react?

To display an image from a local path in React:Download the image and move it into your src directory. Import the image into your file, e.g. import MyImage from './thumbnail. webp' . Set the src prop of the image element to the imported image, e.g. <img src={MyImage} alt="horse" /> .


2 Answers

Hope the following solutions can help you - all can be used for Image

1. HTTPS-Solution:

  1. Your picture is provided by an URI - source={{uri:imageURI}}
  2. Example: source={{uri: 'https://i.vimeocdn.com/portrait/58832_300x300.jpg'}}
  3. Important: Dont forget to set the clip twice: {{}}

2. HTTP-Solution:

  1. If you want http look the following solution - HTTP Github issue

3. Local-Picture

  1. Save: Create a new folder for your images and save them locally there (folder: images)
  2. Require: Require the picture you saved locally by using the among syntax

var yourPicture = require ('./images/picture.jpg');

  • Notation for the same folder ('./')
  • Notation for the above folder ('../')
  • Notation for more folder above ('../../../')
  1. Use: Use your image in the render function
render(){     return(         <Image source={yourPicture}/>     ) } 

The style of your images works as you described

like image 143
Jonathan Stellwag Avatar answered Sep 21 '22 21:09

Jonathan Stellwag


When adding Image tag and use uri you need to check following things:

  • Adding width and height style for Image tag:
    ex:
    <Image source={{uri: 'https://reactjs.org/logo-og.png'}} style={{width: 400, height: 400}} />
    Image doc

  • Using HTTP urls: you will need to enable app transport security
    App transport security for iOS

  • Using HTTPS urls: it will work normally

like image 32
Belal mazlom Avatar answered Sep 22 '22 21:09

Belal mazlom