React-Native图像高度(全宽)

Ale*_*tis 16 image react-native full-width

我有一个图像,我从网址下拉.我提前不知道这张图片的尺寸.

如何设置样式/布局,<Image/>使其成为父视图的整个宽度,并计算高度以保持纵横比?

我尝试onLoad在0.34.0-rc.0中使用,高度/宽度均为0英寸event.nativeEvent.source.

图像是在<ScrollView/>.我不是在全屏图像之后.

nbu*_*urk 19

我的用法非常相似,因为我需要显示全屏宽度保持宽高比的图像.

根据@ JasonGaare的使用答案Image.getSize(),我想出了以下实现:

class PostItem extends React.Component {

  state = {
    imgWidth: 0,
    imgHeight: 0,
  }

  componentDidMount() {

    Image.getSize(this.props.imageUrl, (width, height) => {
      // calculate image width and height 
      const screenWidth = Dimensions.get('window').width
      const scaleFactor = width / screenWidth
      const imageHeight = height / scaleFactor
      this.setState({imgWidth: screenWidth, imgHeight: imageHeight})
    })
  }

  render() {

    const {imgWidth, imgHeight} = this.state

    return (
      <View>
        <Image
          style={{width: imgWidth, height: imgHeight}}
          source={{uri: this.props.imageUrl}}
        />
        <Text style={styles.title}>
          {this.props.description}
        </Text>
      </View>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的代码!我一直在使用组件react-native-fit-image但是加载图像时遇到很多问题,有时无法渲染我对你的代码所做的独特修改是Image的样式是``<Image style = {{width :imgWidth,height:imgHeight}} source = {{uri:this.props.imageUrl}} />`` (2认同)

rfd*_*fdc 11

我是反应原生的新手但我使用简单的风格遇到了这个解决方案:

imageStyle: {
  height: 300,
  flex: 1,
  width: null}
Run Code Online (Sandbox Code Playgroud)

从我的1º应用程序的图像全宽

它对我有用 - 你有什么想法?

提前致谢.

  • 这如何回答原来的问题?你必须知道前方图像的高度...... (4认同)

Jas*_*are 6

React Native具有内置的函数,该函数将返回图像的宽度和高度:Image.getSize()在此处查看文档


Kak*_*shi 6

它对我有用。

import React from 'react'
import { View, Text, Image } from 'react-native'

class Test extends React.Component {
  render() {
    return (
      <View>
        <Image
          source={{ uri: "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQL6goeE1IdiDqIUXUzhzeijVV90TDQpigOkiJGhzaJRbdecSEf" }}
          style={{ height: 200, left: 0, right: 0 }}
          resizeMode="contain"
        />
        <Text style={{ textAlign: "center" }}>Papaya</Text>
      </View>
    );
  }
}

export default Test;
Run Code Online (Sandbox Code Playgroud)

另一种可以在布局事件后获取父视图宽度的方法。

<View 
  style={{ flex: 1}} 
  layout={(event) => { this.setState({ width: event.nativeEvent.layout.width }); }}
/>
Run Code Online (Sandbox Code Playgroud)

当您从布局事件获得宽度父视图时,您可以将宽度分配给图像标签。

import React from 'react';
import { View, Image } from 'react-native';

class Card extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      height: 300,
      width: 0
    };
  }
  render() {
    return (
      <View style={{
        flex: 1,
        flexDirection: 'row'
      }}>
        <View style={{ width: 50, backgroundColor: '#f00' }} />
        <View
          style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fafafa' }}
          onLayout={(event) => { this.setState({ width: event.nativeEvent.layout.width }); }}
        >
          {
            this.state.width === 0 ? null : (
              <Image
                source={{ uri: "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQL6goeE1IdiDqIUXUzhzeijVV90TDQpigOkiJGhzaJRbdecSEf" }}
                style={{ width: this.state.width, height: this.state.height }}
                resizeMode="contain"
              />
            )
          }
        </View>
      </View>
    );
  }
}
export default Card;
Run Code Online (Sandbox Code Playgroud)