[英]Image not found React Native (Expo)
Hello fellow programmers,各位程序员好,
I am trying to show an image using the UserAvatar
component in React-Native (Expo) but I am facing a problem where the link I am getting from the API is not working 404 Not Found
, what is the best possible way to avoid this problem.我正在尝试使用 React-Native (Expo) 中的
UserAvatar
组件显示图像,但我遇到了一个问题,即我从 API 获得的链接不起作用404 Not Found
,避免此问题的最佳方法是什么。 I tried to create a blob using the URL of the image but it was not successful我尝试使用图像的 URL 创建一个 blob,但没有成功
This is the error message i am getting in the app这是我在应用程序中收到的错误消息
Online fetched source is not a supported image at node_modules/react-native-user-avatar/src/helpers.js:41:6 in fetchImage
Here is one of the solutions i have tried:这是我尝试过的解决方案之一:
urlToBlob = (url) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onerror = reject;
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
resolve(xhr.response);
}
};
xhr.open('GET', url);
xhr.responseType = 'blob'; // convert type
xhr.send();
})this.urlToBlob(data)
.then((blob) => {
console.log(blob);
});
The approach that i took to solve this problem is very simple.我用来解决这个问题的方法非常简单。
axios
.get(image_url)
.then((res) => {
if (res.status === 200) {
setImageStatus(200);
}
})
.catch((err) => {
setImageStatus(err.response.status);
});
}
When the response status is 200 then the image exists if not fallback to the default image.当响应状态为 200 时,如果不回退到默认图像,则图像存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.