uniapp 实现生成海报并分享给微信好友和保存到本地相册

记录uniapp 生成二维码海报并保存到本地或者分享给微信好友


前言

最近又遇到一个需求:用户需要将小程序生成的二维码海报分享给微信好友或者保存到本地,最后实现的效果如下:
请添加图片描述请添加图片描述请添加图片描述


一、引入生成二维码的组件

这种网上随便找一下就有了,楼主采用的是tki-qrcode 生成二维码组件,具体的链接如下:
链接: https://blog.csdn.net/qq_45829293/article/details/123169952

二、点击右侧的分享图标生成海报

因为考虑到到时候生成的海报要分享,所以考虑用canvas的方式去绘制海报,当然你也可以试着用传统的 写法去生成,这边楼主没有去尝试过,所以这个方法也就不说了,只说canvas 的方式

核心生成代码如下(示例):

	//初始化画布
			async __init() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				this.ctx = uni.createCanvasContext('my-canvas', this)
				this.canvasW = uni.upx2px(500);
				this.canvasH = uni.upx2px(750);
				//设置画布背景透明
				this.ctx.setFillStyle('rgba(255, 255, 255, 0)')
				//设置画布大小
				this.ctx.fillRect(0, 0, this.canvasW, this.canvasH)
				//绘制圆角背景
				this.drawRoundRect(this.ctx, 0, 0, this.canvasW, this.canvasH, uni.upx2px(18), '#FFFFFF')
				//小程序码
				// let qrcodeImg = await this.getImageInfo(this.qrcode)
				// this.ctx.drawImage(qrcodeImg.path,198,(((this.canvasW-hW) / 2) + hH + 135), 92, 92)
				//获取二维码的图片
				let headerImg = await this.getImageInfo(this.src)
				let hW = uni.upx2px(500);
				let hW1 = uni.upx2px(300);
				let hH = uni.upx2px(300);
				//绘制标题图
				 this.drawRoundImg(this.ctx, headerImg.path, uni.upx2px(100), hH / 4, hW1, hH, 8)
				//绘制提示
				this.ctx.setFontSize(14);
				this.ctx.textAlign = 'center' //文字居中 设置文字居中但是fillText的第二个参数必须为画布宽度一半
				this.ctx.setFillStyle('#A4A4A4');
				let sWidth = this.ctx.measureText(this.subTitle).width
				this.ctx.fillText(this.subTitle, this.canvasW / 2, (
					((this.canvasW - hW) / 2) + hH + 70))
				this.ctx.setFontSize(12);
				this.ctx.fillText(this.subTitle1, this.canvasW / 2, (
					((this.canvasW - hW) / 2) + hH + 90))
				//绘制虚线
				this.drawDashLine(this.ctx, 10, (((this.canvasW - hW) / 2) + hH + 120), (this.canvasW - 10), (((this
					.canvasW - hW) / 2) + hH + 120), 5)
				//左边实心圆
				this.drawEmptyRound(this.ctx, 0, (((this.canvasW - hW) / 2) + hH + 120), 10)
				//右边实心圆
				this.drawEmptyRound(this.ctx, this.canvasW, (((this.canvasW - hW) / 2) + hH + 120), 10)
				//提示文案
				this.ctx.setFontSize(12);
				this.ctx.setFillStyle('#858585');
				//底部广告
				let BottomAdImg = await this.getImageInfo(this.abImg)
				// 判断一下手机系统的宽高
				uni.getSystemInfo({
					success: (res) => {
						if (res.windowHeight <= 568) {
							this.ctx.drawImage(BottomAdImg.path, uni.upx2px(20), (((this.canvasW - hW) /
								2) + hH + 140), uni.upx2px(460), (this.canvasH - hH) / 4)
						} else {
							this.ctx.drawImage(BottomAdImg.path, uni.upx2px(20), (((this.canvasW - hW) /
								2) + hH + 140), uni.upx2px(460), (this.canvasH - hH) / 3)
						}
					}
				});

三:将canvas 图片转化成图片(最关键)

这一步是最关键的,只有这一步完成了,才能够实现分享给用户或者保存下来

代码如下:

this.ctx.draw(true, () => {
						// 将canvas 变成图片方便发送给好友或者保存
						var that = this
						uni.canvasToTempFilePath({
							canvasId: 'my-canvas',
							fileType: 'jpg',
							x: 0,
							y: 0,
							complete: (res) => {
								this.canvasImg = res.tempFilePath
							}
						}, this);
					})

四:保存图片或者发送好友

这里采用了微信原生的方式,在img 标签上加上 show-menu-by-longpress=true 就可以了。

最后

如需代码,请加我邮箱1015095073@qq.com
### UniApp实现商城海报生成功能 在 UniApp 开发环境中,可以通过 `canvas` 绘制来完成商城海报生成支持保存本地相册分享微信好友。以下是具体的技术细节代码示例。 #### 使用 Canvas 组件绘制海报 UniApp 提供了原生的 `<canvas>` 组件用于绘图操作。通过设置不同的属性(如文字、图片、二维码等),可以在画布上构建复杂的图形结构[^1]。 ```html <template> <view class="container"> <!-- 显示生成海报 --> <image :src="posterPath" mode="widthFix"></image> <!-- 触发生成海报按钮 --> <button @click="generatePoster">生成海报</button> <!-- 隐藏的 canvas 元素 --> <canvas style="display:none;" canvas-id="myCanvas"></canvas> </view> </template> ``` #### 数据绑定与方法声明 在 Vue 的数据模型中定义必要的变量,编写逻辑函数处理海报生成过程。 ```javascript <script> export default { data() { return { posterPath: '', // 存储生成后的海报路径 }; }, methods: { generatePoster() { const ctx = uni.createCanvasContext('myCanvas'); // 设置背景颜色 ctx.setFillStyle('#ffffff'); ctx.fillRect(0, 0, 375, 600); // 添加商品图片 ctx.drawImage('/static/product.jpg', 20, 20, 335, 280); // 添加标题文字 ctx.setFontSize(18); ctx.setFillStyle('#333333'); ctx.setTextAlign('center'); ctx.fillText('商品名称', 187.5, 320); // 描述信息 ctx.setFontSize(14); ctx.setFillStyle('#999999'); ctx.fillText('描述:这是一段关于商品的文字说明...', 20, 350); // 底部二维码 ctx.drawImage('/static/qrcode.png', 280, 500, 80, 80); // 完成绘制 ctx.draw(false, () => { this.convertCanvasToImage(); }); }, convertCanvasToImage() { uni.canvasToTempFilePath({ canvasId: 'myCanvas', success: (res) => { this.posterPath = res.tempFilePath; } }); } } }; </script> ``` 上述代码实现了以下功能: - 调用 `createCanvasContext` 方法获取画布上下文对象。 - 利用 `drawImage` `fillText` 方法分别绘制图片文字内容[^3]。 - 将最终渲染好的画布置换为临时文件路径以便于存储或分享。 #### 保存相册 当用户希望将生成海报保存到手机相册时,可调用 `saveImageToPhotosAlbum` API 来完成此操作。 ```javascript methods: { savePoster() { if (!this.posterPath) { uni.showToast({ title: '请先生成海报', icon: 'none' }); return; } uni.saveImageToPhotosAlbum({ filePath: this.posterPath, success(res) { uni.showToast({ title: '保存成功', icon: 'success' }); }, fail(err) { console.error('保存失败:', err); uni.showToast({ title: '保存失败,请重试', icon: 'none' }); } }); } } ``` #### 自定义组件扩展 如果项目需求较为复杂,则推荐封装一个通用的海报生成组件。该组件允许传入动态参数(如商品详情、促销活动文案等),从而灵活调整每张海报的内容布局[^2]。 --- ###
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搞不动的前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值