收藏
回答

canvasToTempFilePath并没有导出指定尺寸的图片?

//压缩开始
    wx.getImageInfo({
      src: item,// 用于图片压缩
      success: res => {
        console.log(res)
        var imageSize = util.imageUtil(res);
        var oldWidth = imageSize.oldWidth;//原图宽度
        var oldHeight= imageSize.oldHeight;//原图高度
        var targetWidth = imageSize.imageWidth;//导出图片宽度
        var targetHeight = imageSize.imageHeight;//导出图片高度
        //设置画布尺寸
        that.setData({
          cw: oldWidth,
          ch: oldHeight
        });


        const ctx = wx.createCanvasContext('ypyasuo');
        ctx.drawImage(item, 0, 0, oldWidth, oldHeight);
        ctx.draw(true, setTimeout(function () {
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: oldWidth,
            height: oldHeight,
            destWidth: targetWidth,
            destHeight: targetHeight,
            canvasId: 'ypyasuo',
            success: function success(res) {
              item = res.tempFilePath
              var sort = i;
              var nowTime = util.formatTime(new Date());
              //上传图片
              //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
              //图片路径可自行修改
              uploadImage(item, 'cbb/' + nowTime + '/',
                function (result) {
                  console.log("======上传成功图片地址为:", result);
                  that.data.imgUrl[sort] = result;


                  if (i == count - 1) {
                    console.log('上传完成')
                    wx.hideLoading()
                    //提交信息到数据库


                    var images = '';
                    for (var ii = 0; ii < that.data.imgUrl.length; ii++) {
                      if (ii > 0) {
                        images += ',' + that.data.imgUrl[ii];
                      } else {
                        images += that.data.imgUrl[ii];
                      }


                    }
                    console.log(images);


                    wx.request({
                      url: app.globalData.baseHost + '/api/member/addyuhuo',
                      data: {
                        user_id: app.globalData.userId,
                        cid: 41,
                        title: that.data.content,
                        images: images,
                        pass: app.globalData.requestpass
                      },
                      method: 'post',
                      success(res) {
                        wx.showToast({
                          title: res.data,
                          icon: 'success',
                          mask: true,
                          duration: 3000
                        })
                        setTimeout(function () {
                          wx.switchTab({ url: '../../my/my' });
                        }, 3000) //延迟时间
                      }
                    })
                  } else {
                    i++
                    console.log('开始传下一个' + i)
                    that.upload(i);
                  }






                }, function (result) {
                  console.log("======上传失败======", result);
                }
              )














            }, fail: function (e) {
              console.log('压缩失败')
            }
          });
        }, 1000));





      }
    });
  },
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签