kiccer/wx-canvas-2d

请调用SaveToAlbum时,可以单独拿到临时路径吗?

Closed this issue · 1 comments

在调用SaveToAlbum的时候,似乎是直接保存了海报,可以只拿到临时路径,使用其他方法比如wx.showShareImageMenu进行保存或者分享吗?

你可以参考文档:开发扩展插件
以及源码:

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 => {

例如开发一个叫 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({
  // 图片参数
})