简体   繁体   English

图像渲染问题 React Native

[英]Image rendering issues React Native

I am building a react native app using mock data.我正在使用模拟数据构建一个 React Native 应用程序。 I am able to render some of the mock data in a flatlist but the image will not render.我能够在平面列表中呈现一些模拟数据,但图像不会呈现。 In the react native devtools it shows the image url but no style is shown even though I have the style in the Stylesheet element.在 react native devtools 中,它显示了图像 url,但没有显示任何样式,即使我在 Stylesheet 元素中有样式。

import React, { Component } from "react";

import { StyleSheet, Text, View, Image, ListView } from "react-native";


class BookItem extends Component {
    render(){
        return (
            <View style = {styles.bookItem}>
                <Image style  = {styles.cover}  /> //is this the issue?
                <View style = {styles.info}>
                    <Text style = {styles.author}>{this.props.author}</Text>
                    <Text style = {styles.title}>{this.props.title}</Text>
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    bookItem: {
        flexDirection: "row",
        backgroundColor: "#FFFFFF",
        borderBottomColor: "#AAAAAA",
        borderBottomWidth: 2,
        padding: 5,
        height: 175
    },
    cover: {
        flex: 1,
        height: 150,
        resizeMode: "contain"
    },
    info: {
        flex: 3,
        alignItems: "flex-end",
        flexDirection: "column",
        alignSelf: "center",
        padding: 20
    },
    author: { fontSize: 18 },
    title: {fontSize: 18, fontWeight: "bold"}
});
export default BookItem

Im not sure if I am using the Image element in react native properly.我不确定我是否在 React Native 中正确使用 Image 元素。 This could be the issue.这可能是问题所在。 For the most part it shows the data in the devtools在大多数情况下,它显示了 devtools 中的数据

You have to use image with source property.您必须使用带有源属性的图像。 If you want to to add a just background you should use just View with background style.如果你想添加一个简单的背景,你应该只使用带有背景样式的View Look at the official doc from here Example is at the below.从这里查看官方文档示例在下面。

<Image
      style={{width: 50, height: 50}}
      source={{uri: 'https://facebook.github.io/react-native/docs/assets/favicon.png'}}
/>

You mentioned a flatlist but the code sample you provided does not have a flatlist.您提到了一个平面列表,但您提供的代码示例没有一个平面列表。

Refer to the documentation for more information.有关更多信息,请参阅文档。
https://facebook.github.io/react-native/docs/image.html https://facebook.github.io/react-native/docs/image.html

Here are the examples provided in the documentation.以下是文档中提供的示例。

<Image
  source={require('/react-native/img/favicon.png')}
/>

<Image
  style={{width: 50, height: 50}}
  source={{uri: 'https://facebook.github.io/react-native/docs/assets/favicon.png'}}
/>

<Image
  style={{width: 66, height: 58}}
  source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
/>

Could you maybe mock up your problem for us?你能为我们模拟一下你的问题吗?
https://snack.expo.io/ https://snack.expo.io/

Two things: Image component needs a source prop to render an image and second, try not to use flex with images.两件事:图像组件需要一个源道具来渲染图像,其次,尽量不要对图像使用 flex。 Usually they need width AND height property manually set to render properly.通常他们需要手动设置宽度和高度属性才能正确呈现。

Change the value for resizeMode to cover instead of contain .为值更改resizeModecover ,而不是contain This has resolved the same issue for me.这为我解决了同样的问题。

I had the same problem, i suggest you update react-native to version 0.63.3 It will probably cause errors, in which case you could use this React-Native upgrade helper.我有同样的问题,我建议你将 react-native 更新到 0.63.3 版本它可能会导致错误,在这种情况下你可以使用这个 React-Native 升级助手。 https://react-native-community.github.io/upgrade-helper/ https://react-native-community.github.io/upgrade-helper/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM