在React Native中,如果图像URL响应为空,如何显示本地占位图像?

huangapple go评论65阅读模式
英文:

How to display local placeholder image if image url response is null in react native

问题

我试图显示一张图片,其源URI来自API调用。如果URI是正确的字符串格式链接,则完全正常工作。但如果URI链接为null或无效链接,我想显示一个本地可用的占位图像(placeholder2)。

代码:

<Image
  style={styles.postImage}
  source={{
    uri: imageUrl,
  }}
  defaultSource={require('../assets/images/placeholder1.png')}
/>

我知道defaultSource用于在真实图像加载时显示临时图像。

我的问题是,如果来自响应的URI为null,我看到了这个错误:

在React Native中,如果图像URL响应为空,如何显示本地占位图像?

但如果我将代码更改为以下内容,我看到空白图像(灰色):

<Image
  style={styles.postImage}
  source={{
    uri: imageUrl || 
    "require('../assets/images/placeholder2.png')",
  }}
/>

在React Native中,如果图像URL响应为空,如何显示本地占位图像?

并在上述代码中添加default source后,我可以看到placeholder1图像而不是placeholder2

<Image
  style={styles.postImage}
  source={{
    uri: imageUrl || 
    "require('../assets/images/placeholder2.png')",
  }}
  defaultSource={require('../assets/images/placeholder1.png')}
/>

有人能帮我修复这个问题吗?
提前感谢。

更新

@louieKim的答案实际上对我有用

<Image source={imageUrl ? {uri: imageUrl} : require('../assets/images/placeholder2.png')} />
英文:

I'm trying to show an image for which the source Uri is received from the API call.
It is working totally fine if the Uri is a correct link in a string format. But if the Uri link is null or not a valid link I would like to show a placeholder image(placeholder2) that is available locally.

Code:

       &lt;Image
          style={styles.postImage}
          source={{
            uri: imageUrl,
          }}
          defaultSource={require(&#39;../assets/images/placeholder1.png&#39;)}
        /&gt;

I know defaultSource is used to show a temporary image when the real image is being loaded

My problem is if the Uri is null from the response, I'm seeing this error

在React Native中,如果图像URL响应为空,如何显示本地占位图像?

But if I change the code to this, I see blank image (gray color)

       &lt;Image
          style={styles.postImage}
          source={{
            uri: imageUrl || 
            &quot;require(&#39;../assets/images/placeholder2.png&#39;)&quot;,
          }}
        /&gt;

在React Native中,如果图像URL响应为空,如何显示本地占位图像?

And upon adding default source to the above code I can see the placeholder1 image instead of placeholder2

       &lt;Image
          style={styles.postImage}
          source={{
            uri: imageUrl || 
            &quot;require(&#39;../assets/images/placeholder2.png&#39;)&quot;,
          }}
          defaultSource = {require(&#39;../assets/images/placeholder1.png&#39;)}
        /&gt;

Can someone help me to fix this?
Thanks in advance.

Update:

Answer by @louieKim actually worked for me

 &lt;Image source={imageUrl ? {uri: imageUrl} : require(&#39;../assets/images/placeholder2.png&#39;)} &gt;

答案1

得分: 1

你可以这样做...

import * as React from 'react';
import { Text, View, StyleSheet, Image, ActivityIndicator } from 'react-native';

const url = 'https://homepages.cae.wisc.edu/~ece533/images/airplane.png';
const placeholder = require('./assets/snack-icon.png');

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      imageSource: null,
    };
  }

  componentDidMount() {
    this.getImageSource(url);
  }

  getImageSource = url => {
    if (url) {
      this.setState({ imageSource: { uri: url } });
    } else {
      this.setState({ imageSource: placeholder });
    }
  };

  onErrorImage = () => {
    this.setState({ imageSource: placeholder });
  };

  render() {
    return (
      <View style={styles.container}>
        <Image
          source={this.state.imageSource}
          style={{ width: 100, height: 100 }}
          onError={this.onErrorImage}
        /> 
      </View>
    );
  }
}

如果我理解正确,请点赞。

英文:

you can do like this...

import * as React from &#39;react&#39;;
import { Text, View, StyleSheet, Image, ActivityIndicator } from &#39;react-native&#39;;

const url = &#39;https://homepages.cae.wisc.edu/~ece533/images/airplane.png&#39;;
const placeholder = require(&#39;./assets/snack-icon.png&#39;);

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      imageSource: null,
    };
  }

  componentDidMount() {
    this.getImageSource(url);
  }

  getImageSource = url =&gt; {
    if (url) {
      this.setState({ imageSource: { uri: url } });
    } else {
      this.setState({ imageSource: placeholder });
    }
  };

  onErrorImage = () =&gt; {
    this.setState({ imageSource: placeholder });
  };

  render() {
    return (
      &lt;View style={styles.container}&gt;
          &lt;Image
            source={this.state.imageSource}
            style={{ width: 100, height: 100 }}
            onError={this.onErrorImage}
          /&gt; 
      &lt;/View&gt;
    );
  }
}


If i am correct, upvote pls.

答案2

得分: 1

你可以简单地在这里遵循老式的方法:

{imageURl!=null && <Image
          style={styles.postImage}
          source={{
            uri: imageUrl 
          }}
        />}
    
{imageURl==null && <Image
          style={styles.postImage}
          source={{
            uri: '../assets/images/placeholder2.png'
          }}
        />}
英文:

You can simply follow the old-school approach here:

 {imageURl!=null &amp;&amp; &lt;Image
          style={styles.postImage}
          source={{
            uri: imageUrl 
          }}
        /&gt;}

{imageURl==null &amp;&amp; &lt;Image
          style={styles.postImage}
          source={{
            uri: &#39;../assets/images/placeholder2.png&#39;
          }}
        /&gt;}

答案3

得分: 1

<Image
style={styles.postImage}
source={{uri: your_image_url !== null ?
your_image_url : '../xxx/your_local_image'}}
}}
/>

英文:

Try this

&lt;Image
  style={styles.postImage}
  source={{uri: your_image_url !== null ? 
    your_image_url : &#39;../xxx/your_local_image&#39;}}
  }}
/&gt;

答案4

得分: 1

<Image
    source={item.image ? { uri: item.image } : Images.no_image}
    style={{ height: '100%', width: '100%', borderRadius: 20 }}
/>
英文:
&lt;Image
    source={item.image ? { uri: item.image } : Images.no_image}
    style={{ height: &#39;100%&#39;, width: &#39;100%&#39;, borderRadius: 20, }}
/&gt;

huangapple
  • 本文由 发表于 2020年1月3日 15:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定