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.
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.
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.
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" /> .
Hope the following solutions can help you - all can be used for Image
1. HTTPS-Solution:
- Your picture is provided by an URI -
source={{uri:imageURI}}
- Example:
source={{uri: 'https://i.vimeocdn.com/portrait/58832_300x300.jpg'}}
- Important: Dont forget to set the clip twice: {{}}
2. HTTP-Solution:
- If you want http look the following solution - HTTP Github issue
3. Local-Picture
- Save: Create a new folder for your images and save them locally there (folder: images)
- Require: Require the picture you saved locally by using the among syntax
var yourPicture = require ('./images/picture.jpg');
('./')
('../')
('../../../')
- Use: Use your image in the render function
render(){ return( <Image source={yourPicture}/> ) }
The style of your images works as you described
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With