Uniapp: H5 压缩图片

  uni.chooseImage...

  uni.getimageInfo({
    src: res.tempFilePaths[0],
    success: (res) => {
      const blobUrl = res.path;
      const canvas = document.createElement("canvas");

      // 减少绘制的大小才能压缩,不然可能压缩后图片会变大
      canvas.width = res.width / 2;
      canvas.height = res.height / 2;

      const ctx = canvas.getContext("2d");
      let img = new Image();
      img.src = blobUrl;
      img.error = uni.hideLoading;
      img.onload = () => {
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        const contentType = "image/png";
        canvas.toBlob(
          async (blob) => {
            const blobUrl2 = window.URL.createObjectURL(blob);
            await _upload(blobUrl2); // 使用uniapp文件上传api发送FormData数据
            window.URL.revokeObjectURL(blobUrl2);
            img = null;
          },
          contentType,
          0.5
        );
      };
    },
    fail: uni.hideLoading,
  });

相关文章

咱们国内现在手机分为两类,Android手机与苹果手机,现在用的...
前言 上一篇做了php的微信登录,所以也总结一下Java的微信授...
前言 上一篇文章写到如何上架IOS应用到Appstore,其中漏掉了...
文件-新建-1.项目 ┌─uniCloud 云空间目录,阿里云为uniClo...
Flexbox #Flex 容器 Flex 是 Flexible Box 的缩写,意为&...
globalStyle 用于设置应用的状态栏、导航条、标题、窗口背景...