【问题标题】:React Native Image not rendering from local address on androidReact Native Image 未从 android 上的本地地址呈现
【发布时间】:2019-08-20 18:16:54
【问题描述】:

我正在学习并做一个关于 react-native 的项目。我在尝试从本地服务器渲染图像时遇到问题。我想出了渲染图像的方法,我面临的问题是来自本地服务器的图像没有在我的应用程序中渲染。我在源代码中添加到 uri 的链接在浏览器中显示图像,但未在应用程序中呈现。我不知道我哪里错了

我已经尝试检查链接是否有效,我使用占位符图像来检查 Image 是否正常工作(它是)。

 <Image source={{uri: 'http://10.0.2.2:8000/packageImages/asd1.jpg'}}
                       style={{width: 400, height: 400}} />

我希望图像可以渲染,但它没有渲染,也没有抛出任何错误。

【问题讨论】:

  • 你确定这是你的本地 ip http://10.0.2.2:8000/ 吗?因为本地网络 ip 从192.168.x.x 开始。如果您确定只是使用ping 10.0.2.2 ping 您的 cmd 或终端,请检查响应。使用目录而不是 url 链接定位路径
  • 来自模拟器的服务器 IP。我可以从地址访问PC服务器
  • 如果你在 react native start 终端有任何错误
  • 反应原生终端也没有这样的错误,它只是一个空白图像
  • 您是否尝试了一些差异图像,例如facebook.github.io/react-native/img/tiny_logo.png。它有效吗?

标签: javascript reactjs react-native


【解决方案1】:

如果您确实希望将 IP 分配给您的模拟器:

adb shell
ifconfig eth0

这会给你类似的东西:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]

How to get the Android Emulator's IP address?

【讨论】:

  • 它说没有这样的设备
  • 尝试 wlan0 而不是 eth0
【解决方案2】:

有时我无法在没有 SSL 加密的情况下渲染图像,您可以尝试使用来自互联网的一些图像吗? 您也可以尝试这种方法来检查您的图像中是否存在某种错误:

class CustomImage extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            source: this.props.source
        }
    }



    shouldComponentUpdate(prevProps, prevState) {
        if (this.state.source !== prevState.source) return true

        return false
    }

    handleImageError = (error) => {

        if (this.props.semImagem) {
            this.setState({
                source: this.props.semImagem
            })
        } else {
            this.setState({
                source: resources.images.livroSemCapa
            })
        }

    }

    render() {

        let theSource = this.state.source

        if (this.state.source ?.uri === null || this.state.source ?.uri ?.trim() === "") {

            theSource = this.props.semImagem ? this.props.semImagem : resources.images.livroSemCapa
        }


        return (
            <Image
                onError={({ nativeEvent: { error } }) => this.handleImageError(error)}
                source={theSource}
                style={this.props.style}
                resizeMode={this.props.resizeMode || 'contain'}
            />
        )
    }
}

【讨论】:

  • 先生,错误似乎表明图像 uri 的流意外结束。我该怎么做才能解决这个问题?有什么建议吗?
  • 您的 URI 上似乎有些错误,您是否尝试使用 SSL 获取一些错误?(HTTPS)
  • 我在本地开发应用,所以没有HTTPS。图片 uri 在 android 浏览器中运行良好。
  • 您使用的是哪个 android API?另外,检查this
  • 这是一个laravel api
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2017-07-21
  • 2021-09-10
  • 2015-12-29
  • 2019-01-29
相关资源
最近更新 更多