简体   繁体   中英

React Native Image Not Working with specific URL

I have the following code below, where the Image component encompasses two Image component.

  <View style={styles.container} >
    <Image
      style={{width: 50, height: 50}}
      source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
    />
          <Image
      style={{width: 50, height: 50}}
      source={{uri: 'http://lghttp.24811.nexcesscdn.net/80B00B/qpb/media/catalog/product/cache/11/image/439x334/9df78eab33525d08d6e5fb8d27136e95/q/w/qw_neverdonejoggers_p_.png'}}
    />
  </View>

For the first github URL it renders the img logo correctly as expected. However for the second Image it doesn't render the source: qw_neverdonejoggers_p_.png

Which leads me to the conclusion that something wrong URL, however clicking on the URL correctly load the Img:

http://lghttp.24811.nexcesscdn.net/80B00B/qpb/media/catalog/product/cache/11/image/439x334/9df78eab33525d08d6e5fb8d27136e95/q/w/qw_neverdonejoggers_p_.png

I attempted to replicate the issue here, https://rnplay.org/apps/_dQXXw but it renders both the images properly.

So its only on the local my computer that for some reason I can render second image?

Using: "react": "15.4.1", "react-native": "^0.39.2",

Andrés' answer is somewhat correct, but it does not address the exact cause of the problem, and that is iOS' App Transport Security. iOS does not allow plaintext requests (http) by default, and so you need to define a 'whitelist' of URLs that can be allowed to override this particular protection mechanism. You have that list already set up so that your app can connect to localhost during development,, so just add new entries to it. You can see how to do so in this answer . Of course, this only works if you know the list of URLs in advance, which might not suit your needs. In that case, have a look at this article .

uri only works with https. In Android it should work fine with either http or https.

You will find further information here .

Guys there is an issue with Image component is that if first time initialize the component with source={{uri: 'some link'}} it may not work (at least not for me when I fetch image from HTTPS URL from Firebase storage). The trick is you have to trigger a change to source props like first you need to keep source to source={undefined} and then change to source={{uri: 'some link'}} . Hope it would help someone

How i make Image source with http or https url work is by resetting the android emulator am using.

You can clear the emulator by opening the android studio then click on tools tab at the top bar select AVD Manager once it open Select the emulator you are using, right click on it then select wipe data. Once that process is completed, open cmd or terminal at the root of your react-native project $ npx react-native run-android

与除了发出HTTPS ,请确保您设置的widthheight的的Image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM