cropper.uvue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <cl-page>
  3. <view class="p-3">
  4. <demo-item :label="t('自定义')">
  5. <cl-button @tap="chooseImage">{{ t("选择图片") }}</cl-button>
  6. <cl-list border :pt="{ className: 'mt-5' }">
  7. <cl-list-item :label="t('调节裁剪框大小')">
  8. <cl-switch v-model="resizable"></cl-switch>
  9. </cl-list-item>
  10. </cl-list>
  11. </demo-item>
  12. </view>
  13. <cl-cropper
  14. ref="cropperRef"
  15. :resizable="resizable"
  16. @crop="onCrop"
  17. @load="onImageLoad"
  18. ></cl-cropper>
  19. </cl-page>
  20. </template>
  21. <script lang="ts" setup>
  22. import { t } from "@/locale";
  23. import DemoItem from "../components/item.uvue";
  24. import { ref } from "vue";
  25. const cropperRef = ref<ClCropperComponentPublicInstance | null>(null);
  26. const resizable = ref(true);
  27. function chooseImage() {
  28. uni.chooseImage({
  29. count: 1,
  30. sizeType: ["original", "compressed"],
  31. sourceType: ["album", "camera"],
  32. success: (res) => {
  33. if (res.tempFilePaths.length > 0) {
  34. cropperRef.value!.open(res.tempFilePaths[0]);
  35. }
  36. }
  37. });
  38. }
  39. function onCrop(result: any) {
  40. console.log("裁剪结果:", result);
  41. uni.showToast({
  42. title: "裁剪完成",
  43. icon: "success"
  44. });
  45. }
  46. function onImageLoad(e: UniImageLoadEvent) {
  47. console.log("图片加载完成", e);
  48. }
  49. onReady(() => {
  50. cropperRef.value!.open('https://uni-docs.cool-js.com/demo/pages/demo/static/bg2.png');
  51. });
  52. </script>