ImageCrop 图片裁剪
裁剪图片
何时使用
裁剪图片
代码演示
基本
最简单的用法。
import ReactDOM from 'react-dom';
import { ImageCrop, Button } from 'choerodon-ui';
import React, { useState, useCallback } from 'react';
const Demo = () => {
const [visable, setVisiable] = useState(false);
const hanleClick = useCallback(() => {
setVisiable(true);
}, []);
const hanleOk = useCallback(({ url, blob, area }) => {
console.log(url, blob, area);
setVisiable(false);
}, []);
const hanleCancel = useCallback(() => {
setVisiable(false);
console.log('close');
}, []);
return (
<>
<Button onClick={hanleClick} funcType="raised">
查看
</Button>
<ImageCrop
modalVisible={visable}
图片裁剪下载
图片裁剪下载。
import ReactDOM from 'react-dom';
import { ImageCrop, Button } from 'choerodon-ui';
import React, { useState, useCallback } from 'react';
const Demo = () => {
const [visable, setVisiable] = useState(false);
const hanleClick = useCallback(() => {
setVisiable(true);
}, []);
const hanleOk = useCallback(({ url, blob, area }) => {
console.log(url, blob, area);
setVisiable(false);
// 这里是获取到的图片base64编码,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果
const imgUrl = url;
// 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片
if (window.navigator.msSaveOrOpenBlob) {
const bstr = atob(imgUrl.split(',')[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
// eslint-disable-next-line no-plusplus
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
// eslint-disable-next-line no-shadow
const blob = new Blob([u8arr]);
window.navigator.msSaveOrOpenBlob(blob, 'chart-download.png');
头像展示
头像编辑。
import ReactDOM from 'react-dom';
import { ImageCrop, Avatar } from 'choerodon-ui';
import React, { useState, useCallback } from 'react';
const { AvatarUploader } = ImageCrop;
const Demo = () => {
const [visable, setVisiable] = useState(false);
const hanleClick = useCallback(() => {
setVisiable(true);
}, []);
const hanleOk = useCallback(() => {
setVisiable(false);
}, []);
const hanleCancel = useCallback(() => {
setVisiable(false);
console.log('close');
}, []);
return (
<>
<Avatar onClick={hanleClick} style={{ backgroundColor: '#87d068' }}>
我绿了
</Avatar>
<AvatarUploader
图片裁剪并上传
图片裁剪并上传,不可以使用upload上传类型和上传前的钩子其他upload的配置可以正常使用。
import React from 'react';
import ReactDOM from 'react-dom';
import { Icon, Modal, ImageCrop, Upload } from 'choerodon-ui';
class PicturesWall extends React.Component {
state = {
previewVisible: false,
previewImage: '',
fileList: [
{
uid: -1,
name: 'xxx.png',
status: 'done',
url:
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
],
};
handleCancel = () => this.setState({ previewVisible: false });
handlePreview = (file) => {
this.setState({
previewImage: file.url || file.thumbUrl,
previewVisible: true,
});
};
图片服务端裁剪
图片服务端裁剪,把完整的裁剪信息和原图传输到服务器。
import React from 'react';
import ReactDOM from 'react-dom';
import { Icon, Modal, ImageCrop, Upload } from 'choerodon-ui';
class PicturesWall extends React.Component {
state = {
previewVisible: false,
previewImage: '',
fileList: [
{
uid: -1,
name: 'xxx.png',
status: 'done',
url:
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
],
};
handleCancel = () => this.setState({ previewVisible: false });
handlePreview = (file) => {
this.setState({
previewImage: file.url || file.thumbUrl,
previewVisible: true,
});
};
裁剪区域自定义渲染
支持在裁剪区域设置想要的渲染样式。
import ReactDOM from 'react-dom';
import { ImageCrop, Button, Row, Col, Avatar } from 'choerodon-ui';
import React, { useState, useCallback } from 'react';
const avatarRender = (src) => {
const prefix = `c7n-image-crop`;
const avatarList = [
{
src,
size: 80,
icon: 'person',
text: '80×80',
},
{
src,
size: 40,
icon: 'person',
text: '40×40',
},
{
src,
size: 20,
icon: 'person',
text: '20×20',
},
];
return src
? avatarList.map(
// eslint-disable-next-line react/jsx-key
(itemProps) => (
<Row
key={itemProps.text}
className={`${prefix}-avatar-row`}
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
aspect | 裁切区域宽高比,width / height | number | 1 / 1 |
shape | 裁切区域形状,‘rect’ 或 ‘round’ | string | ‘rect’ |
grid | 显示裁切区域网格(九宫格) | boolean | false |
zoom | 启用图片缩放 | boolean | true |
rotate | 启用图片旋转 | boolean | false |
beforeCrop | 弹窗打开前调用,若返回 false ,弹框将不会打开 | function | - |
modalTitle | 弹窗标题 | string | ‘编辑图片’ |
modalWidth | 弹窗宽度,像素值或百分比 | number | string | 520 |
modalOk | 弹窗确定按钮文字 | string | ‘确定’ |
modalCancel | 弹窗取消按钮文字 | string | ‘取消’ |
onCancel | 取消模态框触发 | onCancel?: () => void | - |
onOk | 点击模态框确定触发 | { url: string, blob: Blob,area:Area }=> void | - |
modalVisible | 控制模态框的展示 | boolean | ture |
serverCrop | 服务端裁剪 | boolean | false |
onCropComplete | 裁剪完毕触发方法 | ({ url: string, blob: Blob, area: Area }) => void | - |
cropContent | 修改裁剪编辑位置的渲染 | (crop: ReactElement<EasyCropProps>) => React.ReactElement<any> | - |
modalProps | modal 的属性配置不可以更改onOk,onCancel | ModalProps | - |
ImgCrop.AvatarUploader
这个组件可以完成对于头像上传的裁剪的场景需求,主要是增加预览效果集成
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
visible | 上传图片模态框的显示状态 | boolean | false |
onClose | 模态框关闭时的回调 | (visible: boolean) => void; | 无 |
onUploadOk | 成功上传时的回调 | (res: any) => void; | 无 |
uploadUrl | 上传链接 | string | 无 |
uploadFaild | 上传失败 | () => void; | 无 |
uploadError | 上传服务器错误 | (error: any) => void; | 无 |
handleUpload | 点击上传 | (area: AvatarArea) => void; | 无 |
cropComplete | 裁剪完成 | (imageState: any) => void; | 无 |
title | 上传头像标题 | string | React.ReactElement; | 无 |
subTitle | 上传头像小标题 | string | React.ReactElement; | 无 |
previewTitle | 头像预览标题 | string | React.ReactElement; | 无 |
reloadTitle | 重新上传标题 | string | React.ReactElement; | 无 |
uploadProps | 上传配置 | UploadProps | 无 |
modalProps | 模态框的配置 | ModalProps | 无 |
limit | 限制内容 | boolean | {type: ‘jpeg,png,jpg’,size: 1024,} |
previewList | 定义预览的大小 | number[] | [80, 30, 18] |
editorWidth | 裁剪容器宽度 | number | 540 |
editorHeight | 裁剪容器高度 | number | 300 |
minRectSize | 最小裁剪大小 | number | 80 |
defaultRectSize | 默认裁剪大小 | number | 200 |
axiosConfig | axios 上传配置 | AxiosRequestConfig | 无 |
当前内容版权归 Choerodon UI 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Choerodon UI .