更新记录

1.0.0(2026-11-24) 下载此版本

支持各平台,包括鸿蒙端。


平台兼容性

其他

多语言 暗黑模式 宽屏模式 蒸汽模式
√ √ √ ×

gibbs-cropper

高性能移动端图片裁剪组件(Vue3),支持头像裁剪、等比裁剪、自由裁剪,可拖动、缩放、旋转,支持圆角/圆形输出。

平台支持

平台 支持 说明
Android / iOS (App) ✅ 旧版 canvas 绘制
鸿蒙 (APP-HARMONY) ✅ 已适配,画布按需创建
H5 ✅ 输出为 base64 转换后的路径
微信 / 支付宝小程序 ✅ canvas 2d
nvue / uvue ❌ 暂不支持

目录结构

uni_modules/gibbs-cropper/
├── components/gibbs-cropper/
│   ├── gibbs-cropper.vue
│   └── gibbs-cropper.scss
├── static/svg/rotate.svg
├── package.json
└── readme.md

引入与注册

import gCropper from '@/uni_modules/gibbs-cropper/components/gibbs-cropper/gibbs-cropper.vue'

export default {
  components: { gCropper }
}

属性

属性 类型 默认值 说明
imageUrl String '' 待裁剪图片路径。为空时不显示裁剪层
mode String 'ratio' 裁剪模式:fixed / ratio / free
width Number 200 裁剪框宽度(fixed 模式下即输出宽度)
height Number 200 裁剪框高度(fixed 模式下即输出高度)
maxWidth Number 1024 输出图片最大宽度
maxHeight Number 1024 输出图片最大高度
scaleRatio Number 0.7 裁剪框占屏幕比例,建议不超过 0.95
minRatio Number 1 图片最小缩放倍数
maxRatio Number 3 图片最大缩放倍数
radius Number 0 裁剪结果圆角半径,单位 px;仅当裁剪宽高相同且 radius ≥ 输出宽度一半时才输出圆形
isRotateBtn Boolean true 是否显示旋转按钮
delay Number 250 「确定」按钮防抖时间,单位 ms

裁剪模式

值 名称 说明 适用场景
fixed 固定模式 裁剪框尺寸固定,只能拖动/缩放图片 头像、banner、固定比例展示图
ratio 等比模式 锁定宽高比,可拖动裁剪框边角改大小 门头图等比例但尺寸不限
free 自由模式 不限制宽高比 自由裁剪

fixed 与 ratio 都锁定比例;区别在于裁剪框能否缩放。fixed 下 width/height 同时决定比例与输出尺寸,ratio 下只决定比例。

事件

事件 触发时机 返回值
confirm 点击「确定」且裁剪成功 { tempFilePath }
cancel 点击「取消」,或图片加载/裁剪失败 -

完整使用示例

实现:点击图片 → 选图 → 弹出裁剪层 → 确定后上传 → 回填。

模板

<template>
  <view class="page">
    <image :src="logo" mode="aspectFill" class="logo" @click="chooseLogo"></image>

    <g-cropper
      v-if="cropperUrl"
      :imageUrl="cropperUrl"
      mode="fixed"
      :width="400"
      :height="300"
      :maxWidth="400"
      :maxHeight="300"
      :radius="0"
      :delay="250"
      @confirm="onCropperConfirm"
      @cancel="onCropperCancel"
    ></g-cropper>
  </view>
</template>

裁剪层必须放在没有 padding 的父节点下。若父容器有 padding,鸿蒙端 position: fixed 会相对父级偏移,左侧露出白条。

逻辑

import getCommon from '@/common/common.js'
import gCropper from '@/uni_modules/gibbs-cropper/components/gibbs-cropper/gibbs-cropper.vue'

export default {
  components: { gCropper },
  data() {
    return {
      logo: '',
      cropperUrl: '',
      cropperSource: ''
    }
  },
  methods: {
    chooseLogo() {
      uni.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album'],
        success: (res) => {
          const file = (res.tempFiles && res.tempFiles[0]) || {}
          const path = file.path || (res.tempFilePaths && res.tempFilePaths[0]) || ''
          if (!path) {
            uni.showToast({ title: '未获取到图片', icon: 'none' })
            return
          }
          if (file.size && file.size > 10 * 1024 * 1024) {
            uni.showToast({ title: '请上传10M以内的图片', icon: 'none' })
            return
          }
          this.cropperSource = path
          this.cropperUrl = path
        }
      })
    },
    onCropperConfirm(res) {
      const path = (res && res.tempFilePath) || this.cropperSource
      this.cropperUrl = ''
      this.cropperSource = ''
      if (!path) {
        uni.showToast({ title: '裁剪失败,请重试', icon: 'none' })
        return
      }
      uni.showLoading({ title: '上传中', mask: true })
      getCommon.upload(path).then(url => {
        this.logo = url
        uni.hideLoading()
      }).catch(() => {
        uni.hideLoading()
        uni.showToast({ title: '上传失败,请重试', icon: 'none' })
      })
    },
    onCropperCancel() {
      this.cropperUrl = ''
      this.cropperSource = ''
    }
  }
}

注意事项

  1. 不要再叠加 uni.compressImage:组件的 maxWidth/maxHeight 已限制输出尺寸,二次压缩反而损失画质;且 compressImage 的 src 必须是字符串,传 tempFiles[0] 对象会报 Invalid args。
  2. 浮层必须放在无 padding 的父节点下:否则鸿蒙端 position: fixed 会相对父级偏移,左侧露出白条。
  3. 用 v-if 控制挂载:组件内部是 v-show,始终挂载会浪费实例与内存。
  4. 不要写非 scoped 的 page { overflow: hidden }:在 uniapp 中会全局生效,导致页面裁剪一次后整页无法滚动。

提示

该组件的基础是 t-cropper 组件库,因为 t-cropper 不支持鸿蒙端,所以二次开发了这个组件。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

无

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

无

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

无

许可协议

MIT协议