1. 程式人生 > >微信小程式使用canvas在真機上不顯示使用者頭像問題(不顯示網路圖片)

微信小程式使用canvas在真機上不顯示使用者頭像問題(不顯示網路圖片)

如果要做一個分享朋友圈,而需要通過canvas繪製圖片,在真機上是不顯示你的使用者頭像的,但是開發者工具和真機上開啟除錯是可以看的。這就需要在微信公眾平臺把你頭像前面的網址配置到downloadFile網址裡去

比如下面的頭像,就需要把https://wx.qlogo.cn新增到downloadFile網址去,然後清除快取,重新開啟就可以看到繪製的頭像了

"avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/F8sadfIasdasdfadf"},還有這個頭像地址需要下載到本地才能去繪製

openfun() {
      let that= this;
      wx.downloadFile({
        url: that.data.avatarUrl,
        success: function (res) {
          // console.log(res.tempFilePath);
          that.setData({
            avatarUrl:res.tempFilePath,
          })

          //繪圖方法
          that.drawImage();

        }, fail: function (fres) {

        }
      })
    },
    //繪製方法
    drawImage:function() {
      var that = this
      const ctx = wx.createCanvasContext('sharePhoto',this)
      // console.log(wx.getSystemInfoSync())
      var Width = wx.getSystemInfoSync().windowWidth*0.74
      var Height = wx.getSystemInfoSync().windowHeight * 0.68
      var avatarUrl = that.data.avatarUrl
      ctx.save();
      ctx.beginPath();
      ctx.arc(Width * 0.5, Height * 0.175, Width * 0.098, 0, 2 * Math.PI);
      ctx.clip();//畫了圓 再剪下
      ctx.drawImage(avatarUrl, Width * 0.4, Height * 0.109, Width*0.197, Height*0.132)
      ctx.restore();
      ctx.save();
      ctx.draw();
    },