收藏
回答

wx.canvasToTempFilePath预览报错fail invalid viewid?

问题描述:用canvas 2d生成图片后模拟器正常工作,但是用预览却报了fail invalid viewid.报错如下图。

调试基础库:3.0.0;

预览手机:iphone11 ios版本是16.6;

微信版本是8.0.40;



相关代码:

Page({
  data: {
    imageUrl: "",
    canvas: null,
    ctx: null,
  }
  
  onLoad() {
      wx.createSelectorQuery()
        .select('#myCanvas')
        .fields({ node: true, size: true })
        .exec((res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')
        this.setData({canvas, ctx})
      })
  },


  addSelectBox(tempFilePaths) {
    return new Promise( async (resolve, reject) => {
      const imgInfo = await wx.getImageInfo({
          src: this.data.imageUrl
      })
      this.data.canvas.width = imgInfo.width
      this.data.canvas.height = imgInfo.height


      const img = this.data.canvas.createImage()
      img.src = this.data.imageUrl
      
      img.onload=()=>{
        this.data.ctx.drawImage(img, 0, 0,imgInfo.width,imgInfo.height)
        this.data.ctx.beginPath();
        this.data.ctx.lineWidth=3;
        this.data.ctx.strokeStyle="red";
        this.data.ctx.rect(this.data.location.left,
                          this.data.location.top,
                          this.data.location.width,
                          this.data.location.height)
    
        this.data.ctx.stroke();
    
        wx.canvasToTempFilePath({
          canvas: this.data.canvas,
            success: (res) => {
                console.log(res.tempFilePath)
                this.setData({ imageUrl: res.tempFilePath})
            resolve(res.tempFilePath)
          },
          fail: (e)=> {
            console.log(e)
          },
        })
      }
    })
  },
回答关注问题邀请回答
收藏
登录 后发表内容