Closed xuannfx closed 5 months ago
你可以参考文档:开发扩展插件 以及源码: https://github.com/kiccer/wx-canvas-2d/blob/a0c68d1a37a09a2089a3e289a4e70171af8c0f94/plugin/utils/SaveToAlbum.js#L57-L65
例如开发一个叫 getTempFilePath
的扩展方法,可以在 handler
函数中使用 this.canvas
获得 canvas 实例然后调用微信API wx.canvasToTempFilePath
获得临时路径。
// getTempFilePath.js
module.exports = {
name: 'getTempFilePath',
handler (opts) {
return new Promise((resolve, reject) => {
const _opts = {
x: 0,
y: 0,
width: 0,
height: 0,
destWidth: 0,
destHeight: 0,
...opts
}
wx.canvasToTempFilePath({
x: _opts.x,
y: _opts.y,
width: _opts.width,
height: _opts.height,
destWidth: this.xDpr(_opts.destWidth),
destHeight: this.xDpr(_opts.destHeight),
canvas: this.canvas,
success: res => {
const tempFilePath = res.tempFilePath
resolve(tempFilePath)
},
fail: () => {
this.debugLogout('获取临时图片路径失败', 'error')
reject(Error('获取临时图片路径失败'))
}
})
})
}
}
import {
WxCanvas2d
} from 'wx-canvas-2d'
import getTempFilePath from 'path/to/getTempFilePath.js'
WxCanvas2d.use(getTempFilePath)
const canvas = new WxCanvas2d()
const url = await canvas.getTempFilePath({
// 图片参数
})
在调用SaveToAlbum的时候,似乎是直接保存了海报,可以只拿到临时路径,使用其他方法比如wx.showShareImageMenu进行保存或者分享吗?