英文:
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,我看到了这个错误:
但如果我将代码更改为以下内容,我看到空白图像(灰色):
<Image
style={styles.postImage}
source={{
uri: imageUrl ||
"require('../assets/images/placeholder2.png')",
}}
/>
并在上述代码中添加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:
<Image
style={styles.postImage}
source={{
uri: imageUrl,
}}
defaultSource={require('../assets/images/placeholder1.png')}
/>
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
But if I change the code to this, I see blank image (gray color)
<Image
style={styles.postImage}
source={{
uri: imageUrl ||
"require('../assets/images/placeholder2.png')",
}}
/>
And upon adding default source
to the above code I can see the placeholder1
image instead of placeholder2
<Image
style={styles.postImage}
source={{
uri: imageUrl ||
"require('../assets/images/placeholder2.png')",
}}
defaultSource = {require('../assets/images/placeholder1.png')}
/>
Can someone help me to fix this?
Thanks in advance.
Update:
Answer by @louieKim actually worked for me
<Image source={imageUrl ? {uri: imageUrl} : require('../assets/images/placeholder2.png')} >
答案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 '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>
);
}
}
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 && <Image
style={styles.postImage}
source={{
uri: imageUrl
}}
/>}
{imageURl==null && <Image
style={styles.postImage}
source={{
uri: '../assets/images/placeholder2.png'
}}
/>}
答案3
得分: 1
<Image
style={styles.postImage}
source={{uri: your_image_url !== null ?
your_image_url : '../xxx/your_local_image'}}
}}
/>
英文:
Try this
<Image
style={styles.postImage}
source={{uri: your_image_url !== null ?
your_image_url : '../xxx/your_local_image'}}
}}
/>
答案4
得分: 1
<Image
source={item.image ? { uri: item.image } : Images.no_image}
style={{ height: '100%', width: '100%', borderRadius: 20 }}
/>
英文:
<Image
source={item.image ? { uri: item.image } : Images.no_image}
style={{ height: '100%', width: '100%', borderRadius: 20, }}
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论