总结

文档
1. 使用wx.getImageInfo()
将头像+二维码转临时路径
beforCreateImage() {
let that = this
wx.getImageInfo({
src: that.data.avatarUrl,
success: res => {
const avatarUrl = res.path
wx.getImageInfo({
src: that.data.userInfo.showPic,
success: res2 => {
const qrUrl = res2.path
const text = that.data.userInfo.name + that.data.userInfo.phone
that.createImage(avatarUrl, qrUrl, text)
},
fail: err => console.log(33, err)
})
},
fail: err => console.log(22, err)
})
}
2. 绘制海报 -- 用到的路径都是本地路径
<canvas canvas-id="myCanvas" style="width: 300px;height: 400px;position: fixed;top: -10000px;"></canvas>
createImage(avatarUrl, qrUrl, text) {
let that = this
const ctx = wx.createCanvasContext('myCanvas')
ctx.drawImage('/img/qr_code_bg.png', 0, 0, 300, 400)
ctx.save()
ctx.beginPath()
ctx.arc(80/2+110, 80/2+50, 40, 0, 2 * Math.PI, false)
ctx.clip()
ctx.drawImage(avatarUrl, 110, 50, 80, 80);
ctx.restore()
ctx.setFillStyle('#000')
ctx.font = 'normal bold 18px sans-serif';
ctx.fillText(text, 70, 160)
ctx.save()
ctx.beginPath()
ctx.arc(120/2+90, 120/2+186, 60, 0, 2 * Math.PI, false)
ctx.clip()
ctx.drawImage(qrUrl, 90, 186, 120, 120);
ctx.restore()
ctx.setFillStyle('#000')
ctx.font = 'normal normal 14px sans-serif';
ctx.fillText('找工作 职业培训一站式服务平台', 50, 350)
ctx.draw()
}
3. 点击保存,使用wx.canvasToTempFilePath()
将canvas转为图片
saveHaiBaoImage() {
let that = this
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
success: res => {
var tempFilePath = res.tempFilePath;
that.shareImage(tempFilePath)
},
fail:res => {
console.log(res);
}
});
}, 100);
}
4. 使用wx.saveImageToPhotosAlbum()
保存图片
async shareImage(url) {
let that = this
wx.getSetting({
success: res => {
if (res.authSetting['scope.writePhotosAlbum']) {
that.saveImage(url);
} else if (res.authSetting['scope.writePhotosAlbum'] !== undefined){
that.openSetting();
} else {
that.saveImage(url);
}
},
fail: () => {
that.toastComponent.showToastComponent('获取授权信息失败')
}
})
},
openSetting() {
wx.showModal({
title: '提示',
content: '请先授权同意保存图片到相册',
confirmText: '确定授权',
success: res => {
if (res.confirm) {
wx.openSetting({
fail: () => {
that.toastComponent.showToastComponent('打开授权设置页面失败')
}
});
}
}
})
},
saveImage(url) {
let that = this
wx.saveImageToPhotosAlbum({
filePath: url,
success: () => {
this.setData({
isShow: false
})
that.toastComponent.showToastComponent('保存成功')
},
fail: () => {
that.toastComponent.showToastComponent('保存失败')
}
})
},
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)