import React from 'react';
|
// import { Link } from 'react-router-dom';
|
// import { Modal } from 'antd-mobile';
|
// import { Icon } from 'antd';
|
|
import './index.scss';
|
|
export default class AttachmentDisplay extends React.PureComponent {
|
constructor(props) {
|
super(props);
|
this.state = {
|
path: props.path
|
};
|
}
|
rotateImage({ degree, path }) {
|
if (degree && !this.rotating) {
|
this.rotating = true;
|
const canvas = document.createElement('canvas');
|
const contex = canvas.getContext('2d');
|
const image = new Image();
|
image.addEventListener('load', () => {
|
const height = image.height;
|
const width = image.width;
|
if (degree === 180) {
|
canvas.width = width;
|
canvas.height = height;
|
} else {
|
canvas.width = height;
|
canvas.height = width;
|
}
|
contex.rotate(degree * Math.PI / 180);
|
if (degree === 90) {
|
contex.drawImage(image, 0, -height);
|
} else if (degree === 180) {
|
contex.drawImage(image, -width, -height);
|
} else {
|
contex.drawImage(image, -width, 0);
|
}
|
const base64 = canvas.toDataURL('image/jpeg');
|
this.rotating = false;
|
if (this.props.degree === degree) {
|
this.setState({ path: base64 });
|
} else this.rotateImage(this.props);
|
});
|
image.src = path;
|
} else this.setState({ path });
|
}
|
|
componentWillReceiveProps(props) {
|
if (this.props.degree !== props.degree) {
|
this.rotateImage(props);
|
}
|
}
|
|
render() {
|
const { path } = this.state;
|
return (
|
<div className="attachment-display-main">
|
<img src={path} style={{
|
maxWidth: "100%"
|
}} />
|
</div>
|
);
|
}
|
|
}
|