Maintain aspect ratio of image with full width in React Native

前端 未结 12 1465
终归单人心
终归单人心 2020-12-07 13:02

I have a query regarding tag. I want an image to take entire width of parent which I do using alignSelf:stretch, but I also want the height to be according to the aspect ra

相关标签:
12条回答
  • 2020-12-07 13:38
    <Image
       source={require('../../assets/img/headers/image-1.jpg')}
       style={styles.responsiveImage}
     />
    
    const styles = StyleSheet.create({
    
      responsiveImage: {
        width: '100%',
        // Without height undefined it won't work
        height: undefined,
        // figure out your image aspect ratio
        aspectRatio: 135 / 76,
      },
    
    });
    
    0 讨论(0)
  • 2020-12-07 13:38

    In my case, I am using Styled Components in my React Native (v0.62+) project.

    I needed to specify a square aspectRatio for Image components that had a defined width and undefined height.

    I found that styling height:0; achieved the "square image" result that I wanted:

    // Gallery container styled-component
    const Gallery = styled.View`
      flexDirection:row;
      flexWrap:wrap;
    `
    
    // Square half-width image styled-component
    const Photo = styled.Image`
      width:50%;
      height:0;
      aspectRatio:1;
    `
    

    This method also works for full width image styling - replacing width:50% with width:100% produces the expect result with correct aspect ratio of each image.

    0 讨论(0)
  • 2020-12-07 13:43

    It's actually pretty simple.

    The Image class has a getSize method. [1]

    Let's say that you've created a component for your aspectRatioImage and you calculate the appropriate values every time componentWillMount fires.

    Then your code would look something like this:

    componentDidMount() {
        Image.getSize(this.props.source.uri, (srcWidth, srcHeight) => {
          const maxHeight = Dimensions.get('window').height; // or something else
          const maxWidth = Dimensions.get('window').width;
    
          const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
          this.setState({ width: srcWidth * ratio, height: srcHeight * ratio });
        }, error => {
          console.log('error:', error);
        });
      }
    

    So now that the image height and width are saved in your component's state, you can just run

     <Image
       style={{ width: this.state.width, height: this.state.height }}
       source={this.props.source}
       resizeMode="cover"
     />
    

    [1] - https://facebook.github.io/react-native/docs/image.html#getsize

    0 讨论(0)
  • 2020-12-07 13:43

    Use resizeMode='contain'

    <Image style={{height:'100%', width:'100%'}} resizeMode="contain" source={{uri:this.state.imgSource}} />
    

    This will keep the original aspect ratio, with given width and height as max-height and max-width.

    0 讨论(0)
  • 2020-12-07 13:44

    You can calculate the image height based on the width/height ratio.

    So if the image originally is 200x100, after setting its resizeMode to stretch:

    var deviceWidth: Dimensions.get('window').width;
    
    ...
    
    myImage {
        width: deviceWidth,
        height: deviceWidth * 0.5
    }
    

    I know maybe it is not the best practice, but it helped me a lot with images of all sizes that needed to mantain a certain relation with other images, etc.

    0 讨论(0)
  • 2020-12-07 13:44

    In my case i also had to set height to 'auto' :

    {
        width: 200,
        height: 'auto',
        aspectRatio: 16 / 9,
    }
    
    0 讨论(0)
提交回复
热议问题