简体   繁体   English

React Native:从本地主机服务 URI 不呈现图像

[英]React Native: Serving URI from localhost not rendering the image

I'm trying to display my uploaded image saved from to my backend folder.我正在尝试显示从我的后端文件夹保存的上传图像。 Its on different file so to retrieve I have to call it http://localhost:3333/userprofile/5/5.jpeg .它在不同的文件上,所以要检索我必须调用它http://localhost:3333/userprofile/5/5.jpeg But it does not work on my react native code, I tested it on both android and IOS但它不适用于我的反应本机代码,我在 android 和 IOS 上都对其进行了测试

<Thumbnail
    small
    source={{
        uri: `http://localhost:3333/userprofile/${this.props.user.id}/${this.props.user.avatar}`
    }}
/>

it works when I open it on browser.当我在浏览器上打开它时它可以工作。 It also work if I use different url like https://picsum.photos/200/300 how to deal with this?如果我使用不同的 url (例如https://picsum.photos/200/300) ,它也可以工作如何处理?

From the docs ,文档中,

Many of the images you will display in your app will not be available at compile time, or you will want to load some dynamically to keep the binary size down.您将在应用程序中显示的许多图像在编译时将不可用,或者您需要动态加载一些图像以减小二进制大小。 Unlike with static resources, you will need to manually specify the dimensions of your image.与 static 资源不同,您需要手动指定图像的尺寸。 It's highly recommended that you use https as well in order to satisfy App Transport Security requirements on iOS.强烈建议您同时使用 https 以满足 iOS 的应用程序传输安全要求。

You need to provide dimensions for your image.您需要为图像提供尺寸。

<Thumbnail
    small
    source={{
        uri: `http://localhost:3333/userprofile/${this.props.user.id}/${this.props.user.avatar}`
    }}
    style={{width: 400, height: 400}} //provide dimensions 
/>

If you're using android emulator you have to use 10.0.2.2 to access localhost on your PC.如果您使用的是 android 仿真器,则必须使用10.0.2.2访问 PC 上的 localhost。 and if you're using a real device then you need to be on the same wifi network and have to use your PC's ip instead of localhost如果您使用的是真实设备,那么您需要在同一个 wifi 网络上,并且必须使用您 PC 的 ip 而不是localhost

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

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