使用 React 获取图像的尺寸

IT技术 javascript reactjs
2021-05-24 16:37:34

我需要使用 React 获取图像的尺寸。我找到了一个名为react-measure的库,它可以计算 React 组件的测量值。它有效,但我无法在图像加载后触发它。我需要在图像加载时触发它,这样我才能获得准确的尺寸,而不是 0 x 157 或类似的尺寸。

我尝试使用onLoad图像事件来检测图像何时加载,但没有得到满意的结果。基本上我所做的是当图像加载(handleImageLoaded()已调用)时,将hasLoadedstate 属性更改true. 我知道一个事实hasLoaded已更改为,true因为它是这样说的:Image Loaded: true

我注意到的是,我只能计算已缓存图像的尺寸...

这是一个演示视频:cl.ly/250M2g3X1k21

有没有更好、更简洁的方法来使用 React 正确检索维度?

这是代码:

import React, {Component} from 'react';
import Measure from '../src/react-measure';
class AtomicImage extends Component {

    constructor() {
        super();
        this.state = {
            hasLoaded: false,
            dimensions: {}
        };
        this.onMeasure = this.onMeasure.bind(this);
        this.handleImageLoaded = this.handleImageLoaded.bind(this);
    }

    onMeasure(dimensions) {
        this.setState({dimensions});
    }

    handleImageLoaded() {
        this.setState({hasLoaded: true});
    }

    render() {

        const {src} = this.props;
        const {hasLoaded, dimensions} = this.state;
        const {width, height} = dimensions;

        return(
            <div>
                <p>Dimensions: {width} x {height}</p>
                <p>Image Loaded: {hasLoaded ? 'true' : 'false'}</p>
                <Measure onMeasure={this.onMeasure} shouldMeasure={hasLoaded === true}>
                    <div style={{display: 'inline-block'}}>
                        <img src={src} onLoad={this.handleImageLoaded}/>
                    </div>
                </Measure>
            </div>
        );
    }
}

export default AtomicImage;

这是父代码。这并不重要——只是传递srcAtomicImage元素:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import AtomicImage from './AtomicImage';

class App extends Component {

  constructor() {
    super();
    this.state = {src: ''}
    this.handleOnChange = this.handleOnChange.bind(this);
  }

  handleOnChange(e) {
    this.setState({src: e.target.value});
  }

  render() {

    const {src} = this.state;
    return (
      <div>
        <div>
          <input onChange={this.handleOnChange} type="text"/>
        </div>
        <AtomicImage src={src} />
      </div>
    )
  }

}

ReactDOM.render(<App />, document.getElementById('app'));
1个回答

检索维度的方法

你可以通过 js 实现你的目标:通过 offsetHeight、offsetWidth。为了获得 img 的尺寸,img 必须是可见的。您无法从缓存的 img 中获取尺寸。

示例:https : //jsbin.com/jibujocawi/1/edit?js,output

class AtomicImage  extends Component {
   constructor(props) {
        super(props);
        this.state = {dimensions: {}};
        this.onImgLoad = this.onImgLoad.bind(this);
    }
    onImgLoad({target:img}) {
        this.setState({dimensions:{height:img.offsetHeight,
                                   width:img.offsetWidth}});
    }
    render(){
        const {src} = this.props;
        const {width, height} = this.state.dimensions;

        return (<div>
                dimensions width{width}, height{height}
                <img onLoad={this.onImgLoad} src={src}/>
                </div>
               );
    }
}