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.
However, if I use local image instead, the image will be shown.
var Picture = require('./content.jpeg')
In render()
function:
<Image source={Picture} style={styles.thumbnail} />
How can I show picture using URI as source?
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.
You can specify the device the simulator should run with the --simulator flag, followed by the device name as a string. The default is "iPhone 13" . If you wish to run your app on an iPhone SE (2nd generation), run npx react-native run-ios --simulator='iPhone SE (2nd generation)' .
Open App. js and add the following code: import { StatusBar } from "expo-status-bar"; import React from "react"; import { StyleSheet, Text, View, ImageBackground } from "react-native"; const staticImage = require("./src/assets/static-image. png"); export default function App() { return ( <View style={styles.
URI is an address like: http://www.google.com/image.png it refers to the image somewhere. So you can attach a file that you don't have on your device and you don't want to download it.
The problem is that your are trying to load the image from a http connection and not from a https connection as it is demanded by Apple.
Try if your code works with another uri which uses https instead of http. In Android it should work fine with either http or https.
Read more at https://github.com/facebook/react-native/issues/8520 and WWDC 2016: Apple to require HTTPS encryption on all iOS apps by 2017.
If you really want to load something over http you can edit the info.plist file and add your exception there. More detailed info here: Configuring App Transport Security Exceptions in iOS 9 and OSX 10.11.
See also my StackOverflow question here: React-native loading image over https works while http does not work.
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