华为META9机器canvas.drawImage后canvasToTemp图片大小不对?
发布于 5 年前 作者 bgong 8077 次浏览 来自 官方Issues

机型:HUAWEI META9

机型:HUAWEI META9 MHA-AL00

微信版本号:7.0.7

系统版本号:8.0.0

能复现问题的代码片段:

wx.getImageInfo({
        src: imagePath,
        success: function (res) {
 
          // 生成指定宽度的图片
          that.setData({
            canvasWidth: canvasWidth,
            canvasHeight: canvasHeight 
          })
          var ctx = wx.createCanvasContext('tmpImage');
          ctx.drawImage(imagePath, 0, 0, canvasWidth, canvasHeight );
          ctx.draw(false, function() {
            //保存临时文件
            wx.canvasToTempFilePath({
              canvasId: 'tmpImage',
              fileType: 'jpg',
              success: function (res) {
                console.log(res.tempFilePath)
              },
              fail: function (error) {
                console.log(error)
                reject(error)
              }
            })
          })
        }
      }) 
    })

2 回复

原因是:pixelRatio的问题,需要转换下像素,例如图片想要缩放为:500,则实际转换到设备上的像素为=500 / pixelRatio

回到顶部