uni-app 压缩图片上传到服务器

我这里做的是,用户拍照时的图片压缩(选择图片同理),直接上代码:

buttonclickTwo: function () {
      var that = this
      uni.showActionSheet({
        itemList: ['拍照'],
        itemColor: '',
        success: function (res) {
          if (!res.cancel) {
            console.log(res.tapIndex);
            if (res.tapIndex === 0) {
              //拍照
              uni.chooseImage({
                count: 1,
                sourceType: ['camera'],
                success: (chooseImageRes) => {
                  uni.showLoading({
                    title: '加载中'
                  });
                  console.log('imgsss',chooseImageRes.tempFiles)
                  var imgPath = chooseImageRes.tempFiles[0].path
                  console.log(imgPath)
                  // 判断原图是否大于100k  压缩
                  if(chooseImageRes.tempFiles[0].size>=100000) {
                    // uniapp 压缩只支持jpg  故判断
                    if(imgPath.endsWith('png')) {
                      console.log('是png图片')
                      imgPath.replace(/png/g,'jpg')
                    }
                    uni.compressImage({
                      src: imgPath,
                      quality: 80,
                      success: res => {
                        console.log('压缩获得图片地址',res.tempFilePath)
                        imgPath = res.tempFilePath
                        //自己写的上传方法
                        that.uploadImg(imgPath)
                      }
                    })
                  }else {
                  //自己写的上传方法
                    that.uploadImg(imgPath)
                  }
                }
              });
            }
          }
        },
        fail: function (res) {
        },
        complete: function (res) {
        },
      });
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值