ImageBackground 从 API 获取时未显示图像。

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

ImageBackground not displaying image when fetched from API

问题

当我从API获取图像源时,即使源存在,图像也不显示在ImageBackground中,但其他数据如列表标题和其他细节会显示。

import React, { useState, useEffect } from 'react';
import {
  FlatList,
  ImageBackground,
  Image,
  StyleSheet,
  View,
  TouchableOpacity,
} from 'react-native';
import { Paragraph } from './paragraph';
import { Heading } from './heading';
import { baseStyle, images, theme, routes } from '../config';
import { useNavigation } from '@react-navigation/native';

export const Carousal = ({ data, style, onPress }) => {
  const navigation = useNavigation();

  return (
    <FlatList
      horizontal
      style={style}
      data={data}
      showsHorizontalScrollIndicator={false}
      renderItem={({ item }) => (
        <TouchableOpacity
          onPress={() => navigation.navigate(routes.VENUE_PROFILE, { item: item })}
        >
          <ImageBackground
            source={{ uri: item.listing_image }}
            style={styles.flatList}
            resizeMode="contain"
          >
            <Heading style={styles.heading}>{item.listing_title}</Heading>
            <View
              style={{
                ...styles.flex,
                marginVertical: baseStyle.marginVertical(5),
              }}
            >
              <Paragraph style={styles.para}>{item.listing_area}</Paragraph>
              <View style={styles.flex}>
                <Image
                  style={{ ...styles.img, tintColor: theme.colors.black }}
                  source={images.starIcon}
                  resizeMode="contain"
                />
                <Paragraph style={styles.para}>{item.listing_rating}</Paragraph>
              </View>
            </View>
            <View style={styles.flex}>
              <View style={styles.flex}>
                <Image
                  style={{ ...styles.img, tintColor: theme.colors.black }}
                  source={images.pinIcon}
                  resizeMode="contain"
                />
                <Paragraph style={styles.para}>{item.listing_category}</Paragraph>
              </View>
              <View style={styles.flex}>
                <Image
                  style={{ ...styles.img, tintColor: theme.colors.black }}
                  source={images.clockIcon}
                  resizeMode="contain"
                />
                <Paragraph style={styles.para}>{item.listing_time}</Paragraph>
              </View>
            </View>
          </ImageBackground>
        </TouchableOpacity>
      )}
    />
  );
};

const styles = StyleSheet.create({
  flatList: {
    paddingTop: baseStyle.paddingTop(84),
    paddingLeft: baseStyle.paddingLeft(15),
    paddingBottom: baseStyle.paddingBottom(15),
    paddingRight: baseStyle.paddingRight(44),
    marginVertical: baseStyle.marginVertical(10),
    marginRight: baseStyle.marginRight(10),
    width: baseStyle.width(315),
    height: 200,
  },
});

以上代码中只显示了所需的样式。API响应:

{"id": 20, "listing_area": "Korangi", "listing_category": "Food", "listing_description": "Good in Taste", "listing_feedback": 5, "listing_image": "https://www.pexels.com/photo/white-walls-of-buildings-in-town-14200161/", "listing_location": "https://goo.gl/maps/zRVGiY7sBJxseJJCA?coh=178571&entry=tt", "listing_rating": 4, "listing_time": 10, "listing_title": "Jahanzaib Haleem Korangi Branch"}
英文:

When I am fetching image source from API, Image is not displayed in ImageBackground even though source exists but remaining data is displayed such as listing title and other details.

import React, {useState, useEffect}from &#39;react&#39;;
import {
FlatList,
ImageBackground,
Image,
StyleSheet,
View,
TouchableOpacity,
} from &#39;react-native&#39;;
import { Paragraph } from &#39;./paragraph&#39;;
import { Heading } from &#39;./heading&#39;;
import { baseStyle, images, theme, routes } from &#39;../config&#39;;
import { useNavigation } from &#39;@react-navigation/native&#39;;
export const Carousal = ({ data, style, onPress }) =&gt; {
const navigation = useNavigation();
// const [imageUri, setImageUri] = useState(null);
// useEffect(() =&gt; {
//   fetchImage();
// }, []);
// const fetchImage = async () =&gt; {
//   try {
//     const response = await fetch (`$item.listing_image`); // Replace with your image URI
//     const blob = await response.blob();
//     const uri = URL.createObjectURL(blob);
//     setImageUri(uri);
//   } catch (error) {
//     console.error(error);
//   }
// };
return (
// data.length &gt; 0
// ?
&lt;FlatList
horizontal
style={style}
data={data}
showsHorizontalScrollIndicator={false}
renderItem={({ item }) =&gt; (
&lt;TouchableOpacity onPress={() =&gt; navigation.navigate(routes.VENUE_PROFILE, {item:item})}&gt;
&lt;ImageBackground
source={{uri: item.listing_image}}
style={styles.flatList}
resizeMode=&quot;contain&quot;&gt;
&lt;Heading style={styles.heading}&gt;{item.listing_title}&lt;/Heading&gt;
&lt;View
style={{
...styles.flex,
marginVertical: baseStyle.marginVertical(5),
}}&gt;
&lt;Paragraph style={styles.para}&gt;{item.listing_area}&lt;/Paragraph&gt;
&lt;View style={styles.flex}&gt;
&lt;Image
style={{...styles.img, tintColor: theme.colors.black}}
source={images.starIcon}
resizeMode=&quot;contain&quot;
/&gt;
&lt;Paragraph style={styles.para}&gt;{item.listing_rating}&lt;/Paragraph&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;View style={styles.flex}&gt;
&lt;View style={styles.flex}&gt;
&lt;Image
style={{...styles.img, tintColor: theme.colors.black}}
source={images.pinIcon}
resizeMode=&quot;contain&quot;
/&gt;
&lt;Paragraph style={styles.para}&gt;{item.listing_category}&lt;/Paragraph&gt;
&lt;/View&gt;
&lt;View style={styles.flex}&gt;
&lt;Image
style={{...styles.img,tintColor: theme.colors.black}}
source={images.clockIcon}
resizeMode=&quot;contain&quot;
/&gt;
&lt;Paragraph style={styles.para}&gt;{item.listing_time}&lt;/Paragraph&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;/ImageBackground&gt;
&lt;/TouchableOpacity&gt;
)}
/&gt;
// :
// &lt;Text style={styles.flex}&gt; No Record Found &lt;/Text&gt;
);
};
const styles = StyleSheet.create({
flatList: {
paddingTop: baseStyle.paddingTop(84),
paddingLeft: baseStyle.paddingLeft(15),
paddingBottom: baseStyle.paddingBottom(15),
paddingRight: baseStyle.paddingRight(44),
marginVertical: baseStyle.marginVertical(10),
marginRight: baseStyle.marginRight(10),
width: baseStyle.width(315),
height:200,
},
});

displayed only the required styling in above code.
API response:

{"id": 20, "listing_area": "Korangi", "listing_category": "Food", "listing_description": "Good in Taste
", "listing_feedback": 5, "listing_image": "https://www.pexels.com/photo/white-walls-of-buildings-in-town-14200161/", "listing_location": "https://goo.gl/maps/zRVGiY7sBJxseJJCA?coh=178571&entry=tt", "listing_rating": 4, "listing_time": 10, "listing_title": "Jahanzaib Haleem Korangi Branch"}

答案1

得分: 0

"code is all good but your repones from api
"listing_image": "https://www.pexels.com/photo/white-walls-of-buildings-in-town-14200161/",
is not an image URL it site page URL I use this "https://images.pexels.com/photos/14200161/pexels-photo-14200161.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"
you just need a proper image URI
it might work if didn't use flex in style and check style."

英文:

code is all good but your repones from api
"listing_image": "https://www.pexels.com/photo/white-walls-of-buildings-in-town-14200161/",
is not an image URL it site page URL I use this "https://images.pexels.com/photos/14200161/pexels-photo-14200161.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"
you just need a proper image URI
it might work if didn't use flex in style and check style.

huangapple
  • 本文由 发表于 2023年6月8日 12:58:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428730.html
匿名

发表评论

匿名网友

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

确定