15

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?

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Fredric
  • 1,089
  • 2
  • 10
  • 19

6 Answers6

41

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.

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Orlando
  • 1,576
  • 2
  • 15
  • 20
7

Here is a sample code for Hamburger icon in image-

<Image source={{ uri: 'http://i.imgur.com/vKRaKDX.png', width: 32, height: 32, }} /> 

for more info you can refer here-https://facebook.github.io/react-native/docs/image.html

Dlucidone
  • 1,091
  • 9
  • 23
2

just edit list.info file at field: "NSAppTransportSecurity" into ios of React Native such as:

<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>resizing.flixster.com</key>
        <dict>
            <!--Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
            <!--Include to allow HTTP requests-->
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>
brianha289
  • 338
  • 1
  • 8
0

I have found a solution from another stack overflow question. Its solved my problem was that a image showed in before but some time its not showing in IoS simulator. Below answer is fixed my problem

React-native iOS not showing images (pods issue)

Thanks @gyan deep

Elavarasan r
  • 1,055
  • 2
  • 12
  • 22
-1

Here is a possible way on how to know if your image will be displayed: copy image uri and paste it to you browser (safari, chrome, etc) -> if you do not see your image, then it probably won't displayed by your phone

alivanov
  • 99
  • 6
-2

Have you tried wrapping it in uri: ?

<Image source={{uri: imageURI}} style={styles.thumbnail} />
mstearne
  • 41
  • 5
  • Yes, I wrapped it in uri as I mentioned in the question. I also tried changing the ordering as your answer shows, but the image still doesn't show. – Fredric Jul 01 '16 at 07:05