cropper.uvue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-page>
  14. <cl-cropper
  15. ref="cropperRef"
  16. :resizable="resizable"
  17. @crop="onCrop"
  18. @load="onImageLoad"
  19. ></cl-cropper>
  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(url: string) {
  40. uni.previewImage({
  41. urls: [url]
  42. });
  43. }
  44. function onImageLoad(e: UniImageLoadEvent) {
  45. console.log("onImageLoad", e);
  46. }
  47. </script>