weapp-qrcode
weapp-qrcode copied to clipboard
把二维码封装成组件的时候无法导出图片,而且导出图片的时候进入的是初始化方法的callback
1、exportImage的方法我改成了这个,这样保存图片的时候才能够保存成功,如果canvas封装成组件的话,canvasToTempFilePath要传入当前canvas的实例
QRCode.prototype.exportImage = function (callback) { if (callback && typeof callback === 'function') {
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: this._htOption.width,
height: this._htOption.height,
destWidth: this._htOption.width,
destHeight: this._htOption.height,
canvasId: this.canvasId,
success: (res) => {
callback({path: res.tempFilePath})
}
}, this._htOption.usingIn)
}
}
2、另外,在初始化的方法内,生成图片本地链接的方法改成了这个,增加了this._htOption.callback的回调 _oContext.draw(false, () => { setTimeout(() => { this.exportImage(this._htOption.callback) }, 800) })
if (this._htOption.usingIn) { wx.canvasToTempFilePath({ x: 0, y: 0, width: this._htOption.width, height: this._htOption.height, destWidth: this._htOption.width, destHeight: this._htOption.height, canvasId: this.canvasId, success: (res) => { // console.log('exportImage2'); this._htOption.callback({path: res.tempFilePath}) }, fail: (res) => { // console.log('exportImage3',res); }, },this._htOption.usingIn) 报错是 canvasToTempFilePath: fail canvas is empty , 报这个错是因为调取 wx.canvasToTempFilePath 接口获取不到canvas,
所以原因是上下文的this,各位自行解决吧