我的代码仅在选择新图像时才有效。但我也想裁剪并保存现有图像。我正在使用react-avatar-edit。
import React from 'react';
import Avatar from 'react-avatar-edit';
class CropAndDrop extends React.Component {
constructor(props) {
super(props);
const src = 'http://i65.tinypic.com/2gwz5s8.jpg';
this.state = {
preview: null,
src,
};
}
onClose = () => {
this.setState({ preview: null });
};
onCrop = preview => {
this.setState({ preview });
let tempImage = this.state.preview;
this.props.preserveImage(tempImage);
// localStorage.setItem('tempImage', this.state.preview);
// localStorage.getItem('tempImage');
};
render() {
return (
<div>
<Avatar
width={600}
height={395}
onCrop={this.onCrop}
onClose={this.onClose}
src={this.state.src}
/>
</div>
);
}
}
export default CropAndDrop;