Javascript 使用 React 获取图像的尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39092859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get dimensions of image with React
提问by Matthew
I need to get the dimensions of an image with React. I found a library called react-measurethat computes measurements of React components. It works, but I can't get it to fire when the image has loaded. I need to get it to fire when the image loads so I get accurate dimensions and not 0 x 157 or something like that.
我需要使用 React 获取图像的尺寸。我找到了一个名为react-measure的库,它可以计算 React 组件的测量值。它有效,但我无法在图像加载后触发它。我需要在图像加载时触发它,这样我才能获得准确的尺寸,而不是 0 x 157 或类似的尺寸。
I tried using the onLoad
Image Event to detect when the image has loaded, but I didn't get satisfactory results. Essentially what I've done is when the image has loaded (handleImageLoaded()
has been called), change the hasLoaded
state property to true
. I know for a fact that hasLoaded
has been changed to true
because it says so: Image Loaded: true
.
我尝试使用onLoad
图像事件来检测图像何时加载,但没有得到满意的结果。基本上我所做的是当图像加载(handleImageLoaded()
已被调用)时,将hasLoaded
state 属性更改为true
. 我知道一个事实hasLoaded
已更改为,true
因为它是这样说的:Image Loaded: true
。
What I noticed is that I can calculate the dimensions for only images that have been cached...
我注意到的是,我只能计算已缓存图像的尺寸...
Here is a demo video: cl.ly/250M2g3X1k21
这是一个演示视频:cl.ly/250M2g3X1k21
Is there a better, more concise way of retrieving dimensions properly with React?
有没有更好、更简洁的方法来使用 React 正确检索维度?
Here is the code:
这是代码:
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;
Here is the parent code. It's not really important—just passes src
to the AtomicImage
element:
这是父代码。这并不重要——只是传递src
给AtomicImage
元素:
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'));
回答by Kokovin Vladislav
way of retrieving dimensions
检索维度的方法
You can achieve your goal just by js: through offsetHeight, offsetWidth. In order to get the img's dimensions, img must be visible. You can't get dimensions from a cached img.
你可以通过 js 实现你的目标:通过 offsetHeight、offsetWidth。为了获得 img 的尺寸,img 必须是可见的。您无法从缓存的 img 中获取尺寸。
example: https://jsbin.com/jibujocawi/1/edit?js,output
示例: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>
);
}
}