简体   繁体   English

React Native Image 不适用于特定的 URL

[英]React Native Image Not Working with specific URL

I have the following code below, where the Image component encompasses two Image component.我在下面有以下代码,其中 Image 组件包含两个 Image 组件。

  <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.对于第一个 github URL 它会按预期正确呈现 img 徽标。 However for the second Image it doesn't render the source: qw_neverdonejoggers_p_.png但是对于第二张图片,它不渲染源代码:qw_neverdonejoggers_p_.png

Which leads me to the conclusion that something wrong URL, however clicking on the URL correctly load the Img:这让我得出结论,URL 有问题,但是点击 URL 正确加载了 Img:

http://lghttp.24811.nexcesscdn.net/80B00B/qpb/media/catalog/product/cache/11/image/439x334/9df78eab33525d08d6e5fb8d27136e95/q/w/qw_neverdonejoggers_p_.png 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.我试图在这里复制这个问题, https://rnplay.org/apps/_dQXXw但它正确地呈现了两个图像。

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",使用:“反应”:“15.4.1”,“反应本机”:“^ 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. Andrés 的回答有点正确,但它没有解决问题的确切原因,那就是 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.默认情况下,iOS 不允许纯文本请求 (http),因此您需要定义一个可以允许覆盖此特定保护机制的 URL 的“白名单”。 You have that list already set up so that your app can connect to localhost during development,, so just add new entries to it.您已经设置了该列表,以便您的应用程序可以在开发期间连接到 localhost,因此只需向其中添加新条目即可。 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.当然,这仅在您事先知道 URL 列表时才有效,这可能不适合您的需要。 In that case, have a look at this article .在这种情况下,请查看这篇文章

uri only works with https. uri仅适用于 https。 In Android it should work fine with either http or https.在 Android 中,它应该适用于 http 或 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).伙计们,图像组件有一个问题是,如果第一次使用source={{uri: 'some link'}}初始化组件,它可能不起作用(至少当我从 HTTPS URL 从 Firebase 存储中获取图像时,它可能不起作用) . 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'}} .诀窍是你必须触发对 source 道具的更改,比如首先你需要将 source 保持为source={undefined}然后更改为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.我如何使用 http 或 https url 工作的图像源是通过重置正在使用的 android 模拟器。

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.您可以通过打开 android studio 清除模拟器,然后单击顶部栏中的工具选项卡,一旦它打开就选择 AVD Manager 选择您正在使用的模拟器,右键单击它,然后选择擦除数据。 Once that process is completed, open cmd or terminal at the root of your react-native project $ npx react-native run-android该过程完成后,在 react-native 项目 $ npx react-native run-android 的根目录打开 cmd 或终端

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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