收藏
回答

wx.canvastotempfilepath 关于 压缩图片大小问题?

请问为什么压缩出来的图片,在个别手机上是一张空白的图片了,代码如下

var Str_Image = function (that) {

  var ctx = wx.createCameraContext()

  ctx.takePhoto({

    quality: 'low',

    success: (res) => {


      console.log(res);

      var ctx = wx.createCanvasContext('photo_canvas');

      var ratio = 2;

      var canvasWidth = res.width

      var canvasHeight = res.height;

      while (canvasWidth > 200 || canvasHeight > 200{

        //比例取整

        canvasWidth = Math.trunc(res.width / ratio)

        canvasHeight = Math.trunc(res.height / ratio)

        ratio++;

      }

      that.setData({

        canvasWidth: canvasWidth,

        canvasHeight: canvasHeight

      }//设置canvas尺寸

      ctx.drawImage(res.tempImagePath, 0, 0, 150, 150//canvasWidth, canvasHeight

      ctx.draw()

      //下载canvas图片

      setTimeout(function () {

        wx.canvasToTempFilePath({

          canvasId: 'photo_canvas',

          x: 0,

          y: 0,

          width: 150,

          height: 150,

          //生成图片的大小

          destWidth: 150,

          destHeight: 150,

          success: function (res) {


            that.setData({

              imageUrl: res.tempFilePath,

            });


            uploadImage(that, res.tempFilePath, that.data.str_UserName//that.data.str_UserName

          },

          fail: function (error) {

            console.log(error)

          }

        })

      }, 100)

    },

    fail: (res) => {

      console.log(res);

    }

  })


}



回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签