1. 绘制之前,先将图片下载到本地,远程图片包含:头像,小程序二维码
wx.downloadFile下载文件资源到本地
2 .远程二维码头像下载到本地后,利用Wxml2Canvas插件绘制成海报,并保存在用户系统相册(wx.saveImageToPhotosAlbum)
插件参考 https://github.com/wg-front/wxml2canvas
async drawImage1 () {
await this.download()//先把图片转为本地图片
let self = this;
var curDrawImage = new Wxml2Canvas({
width: 250, // 宽, 以iphone6为基准,传具体数值,其他机型自动适配
height: 310, // 高
element: 'canvas1',
background: '#f0f0f0',
progress (percent) {
},
finish(url) {
wx.saveImageToPhotosAlbum({
filePath: url,
success(){
wx.hideLoading()
wx.showToast({
title: '保存成功',
icon: 'success',
duration:2000
})
},
fail(err){
wx.showModal({
title: '提示',
content: '您好,请先授权,再保存图片',
showCancel:false,
success: function(res) {
wx.openSetting({
success: (result)=>{
console.log("-----------res",result.authSetting);
if(result.authSetting['scope.writePhotosAlbum']){
wx.saveImageToPhotosAlbum({
filePath:url,
success(){
wx.showToast({
title: '保存成功',
icon: 'success'
})
}
})
}else{
wx.showToast({
title: '温馨提示',
content: '授权失败,请稍后重新获取',
showCancel: false,
})
}
},
fail: ()=>{},
complete: ()=>{}
});
}
})
}
})
},
error (res) {
console.log("失败")
}
});
let data = {
list: [{ //绘制矩形
type: 'rect',
x: 0,
y: 0,
style: {
// width: _this.data.width,
// height: _this.data.height - 60,
width: 250,
// height: 390,
height: 310,
fill: 'transparent'
}
},
{ //根据wxml绘制,
type: 'wxml',
class: '.share__canvas1 .draw_canvas',
limit: '.share__canvas1',
x: 0,
y: 0
}]
}
curDrawImage.draw(data);