自定义分享图片的问题
发布于 4 年前 作者 syao 16641 次浏览 来自 问答
  • 希望在调用onShareAppMessage方法之前通过canvas对图片绘制,生成自定义的分享图片
  • 如何在分享之前对图片进行绘制?

相关代码:

wxml:

<button open-type=‘share’ bindtap=‘myShare’ >分享</button>

<canvas  canvas-id=‘c’ style=‘width:750px;height:400px’></canvas>

js:

onReady: function () {

var that = this;

ctx.drawImage(’/images/test.jpg’, 0, 0, 750, 400);

ctx.setFillStyle(‘red’);

ctx.setFontSize(30)

ctx.fillText(‘hello’, 30, 30);

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

wx.canvasToTempFilePath({

x: 0,

y: 0,

width: 750,

height: 400,

destWidth: 750,

destHeight: 400,

canvasId: ‘c’,

success(res) {

// console.log(res.tempFilePath);

that.setData({

img: res.tempFilePath

})

console.log(that.data.img)

}

})

}, 100))

},

onShareAppMessage: function () {

console.log(this.data.img)

return {

title: ‘这是title’,

imageUrl: this.data.img

}

}

我在onReady里面先绘制好自定义的图片可以实现,但是怎么才能在点击分享按钮时 实现呢?

2 回复

是的,可以在进入页面的时候就开始绘制canvas并保存到缓存,如果是让用户点击按钮导出,那可以设置一个loading,缓存成功了再显示转发按钮

不能再点分享的时候去绘制,来不及,不会等你。。

回到顶部