[微信小程序]fail canvas is empty [fail canvas is empty]

- 当前 Bug 的表现(可附上截图)

选择图片后显示canvasToTempFilePath: fail canvas is empty

- 预期表现

返回success信息

- 复现路径

- 提供一个最简复现 Demo

wx.chooseImage({

//2019.3.2 压缩上传

//count: 1,


sizeType: ['original', 'compressed'],

success: function (res) {

var _that = this;

var path = res.tempFilePaths[0];

that.data.scardimg = res.tempFilePaths;

console.log("the old path:")

console.log(res.tempFilePaths[0])

//-----返回选定照片的本地文件路径列表,获取照片信息-----------

wx.getImageInfo({

src: res.tempFilePaths[0],

success: function (res) {

//---------利用canvas压缩图片--------------

console.log("getinfo")

console.log(res)

var ratio = 2;

var canvasWidth = res.width //图片原始长宽

var canvasHeight = res.height

while (canvasWidth > 400 || canvasHeight > 400){// 保证宽高在400以内

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

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

ratio++;

}

that.setData({

cWidth: canvasWidth,

cHeight: canvasHeight

})

console.log(canvasHeight)

console.log(canvasWidth)

console.log(res.path)

//----------绘制图形并取出图片路径--------------

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

ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight);

//ctx.draw();


//将生成好的图片保存到本地,需要延迟一会,绘制期间耗时

wx.showToast({

title: '图片压缩中...',

icon: 'loading',

duration: 500

});


ctx.draw(false,

setTimeout(function(){

wx.canvasToTempFilePath({

canvasId: 'firstCanvas',

destWidth: canvasWidth,

destHeight: canvasHeight,

success: function (res) {

var tempFilePath = res.tempFilePath;

console.log("success canvas");

that.setData({

scardimg:res.tempFilePaths,

img_arr: that.data.scardimg.concat(res.tempFilePaths),

show_addimg: 0

});

wx.hideToast()

},

fail: function (res) {

console.log(res);

}

})

}, 500));

}

})//info

}//success

})

}