自定义component内使用wx.canvasToTempFilePath报错
发布于 6 年前 作者 xfang 294 次浏览 来自 问答

createPosterCanvas: function (product,qrcode) {

      const self = this

      const ctx = wx.createCanvasContext(‘posterCanvas’, self)

      ctx.drawImage(product, 0, 0, 313, 151)

      ctx.drawImage(qrcode, 200, 170, 100, 100)

      ctx.setFontSize(12)

      ctx.setFillStyle(’#666666’)

      ctx.setTextAlign(‘center’)

      ctx.fillText(‘长按识别二维码’, 246, 292)

      ctx.draw(false,setTimeout(()=>{

        wx.canvasToTempFilePath({

          canvasId: ‘posterCanvas’,

          fileType: ‘jpg’,

          success: function (res) {

            console.log(res.tempFilePath)

          },

          fail: function (res) {

            console.log(res)

          }

        }, self)

      },200))

    },

使用开发者工具可以获取到文件的临时路径,使用手机返回fail canvas is empty。

有没有遇到过类似问题的同学,求帮忙解答一下,谢谢~

3 回复

你的setTimeout的位置不对吧。

ctx.draw(false, () => {
  setTimeout(() => {
    // do something...
  }, 200);
});
回到顶部