Skip to content

mp-cropper 图片裁剪

该组件从 uni-app 插件市场移植,更具体的参数及更新请参考:ksp-cropper

代码演示

vue
<script setup>
import {ref} from 'vue'
import {useToggle} from '@vueuse/core'
import {chooseImage} from '@ifanrx/uni-mp'

const src = ref(null)

const [showCropper, toggleShowCropper] = useToggle(false)

async function uploadImage() {
  const {tempFiles} = await chooseImage(1, ['album'])

  if (!tempFiles.length) return

  const url = tempFiles[0].tempFilePath

  src.value = url

  toggleShowCropper(true)
}

function onCropperOk() {
  toggleShowCropper(false)
}

function onCropperCancel() {
  toggleShowCropper(false)
}
</script>

<template>
  <mp-nav-bar title="mp-cropper" />

  <button @click="uploadImage">upload image</button>

  <view v-if="showCropper">
    <mp-cropper
      ref="cropper"
      borderColor="#cdcdcd"
      buttonColor="#909090"
      mode="free"
      :url="src"
      :width="300"
      @cancel="onCropperCancel"
      @confirm="onCropperOk"
    />
  </view>
</template>

API

Props

Prop nameDescriptionTypeValuesDefault
mode裁剪模式string-'free'
url图片路径string-''
width宽度number-200
height高度number-200
maxWidth最大宽度number-1024
maxHeight最大高度number-1024
borderColor边框颜色string-'#ffffff'
buttonColor确认按钮颜色string-'#42b983'

Events

Event namePropertiesDescription
cancel点击取消时触发
confirmpath string - 图片路径点击确认时触发

源码

组件示例