微信小程序生成分享图片,保存到本地
1.页面
<canvas canvas-id="shareCanvas" style="width:600px;height:900px"></canvas>
2.绘制图片
通过使用wx.downloadFile或wx.getImageInfo
这个API来下载一个网络图片到本地(并可获取该图片的尺寸等其他信息),然后调用ctx.drawImage
方法将图片绘制到画布上,填满画布
wx.downloadFile({
url: 'https://nlwxapp.oss-cn-beijing.aliyuncs.com/static/butiful.png',
success (res) {
if (res.statusCode === 200) {
that.bgImgPath = res.tempFilePath// 背景图
}
}
})
3.小程序码
通过后台接口获得小程序码,将小程序码下载到本地
4.绘制
circleImg(ctx, img, x, y, r){
ctx.save();
var d = 2 * r;
var cx = x + r;
var cy = y + r;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
// ctx.setStrokeStyle('rgba(0,0,0,0)')
canvas.stroke()
ctx.clip();
ctx.drawImage(img, x, y, d, d);
ctx.restore()
},
showImg() {
var that = this;
const ctx = wx.createCanvasContext('myCanvas'); // 设置背景
ctx.setFillStyle('#E72454')
ctx.fillRect(0,0,315,393) // 绘制图片
ctx.drawImage(that.bgImgPath, 0, 0, 315, 250)
// 绘制头像
that.circleImg(ctx,that.headIcon,315/2-50, 20, 50) //创建文字
ctx.setFontSize(18)
ctx.setFillStyle('#fff')
ctx.setTextAlign('center')
ctx.fillText(that.userInfo.nickName + '邀请你一起分奖金', 315 / 2, 140)
ctx.stroke() ctx.setFontSize(42)
ctx.setFillStyle('#FFD288')
ctx.textAlign = 'center'
ctx.fillText(that.message.money, 157, 180) var a = ctx.measureText(that.message.money).width
ctx.setFontSize(16)
ctx.setFillStyle('#FFD288')
ctx.fillText('元', 157 + 5 + a/2, 180)
ctx.stroke() // 绘制小程序码
ctx.drawImage(that.wxCode, 315/2-75, 200, 150, 150) context.setFontSize(12)
context.setFillStyle("#fff")
context.setTextAlign("center")
context.fillText("长按识别小程序", 157, 310)
ctx.stroke()
//渲染
ctx.draw() },
5.将canvas沪指的内容转成图片
//需要把canvas转成图片后才能保存
wx.canvasToTempFilePath({ // 生成图片并把绘制的图片路径保存到一个变量
x: 0,
y: 0,
width: 315,
height: 393,
destWidth: 1260, //2倍关系
destHeight: 1572, //2倍关系
canvasId: 'myCanvas',
success: function (res) {
//关键 赋值给变量
that.shareImgSrc = res.tempFilePath
that.saveImg2()
},
fail: function (res) {
console.log(res)
}
})
6.将图片保存到本地
var that = this
wx.saveImageToPhotosAlbum({ // 这张图片保存到本地相册
//shareImgSrc为canvas赋值的图片路径
filePath: that.shareImgSrc,
success(res) {
console.log('保存成功');
wx.showModal({
title: '保存成功',
content: '图片成功保存到相册了,快去发朋友圈吧~',
showCancel: false,
confirmText: '确认',
confirmColor: '#21e6c1',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
})
}
})
完整代码
<template>
<div>
<canvas canvas-id="myCanvas" :class="{'topScroll':isOpacity}"></canvas>
</div>
</template> <script>
export default {
components: {
},
props: {
},
data() {
return {
isOpacity: true,
deviceWidth: 0,
deviceHeight: 0,
shareImgSrc: '',
bgImgPath: '',
headIcon:'',
wxCode: ''
}
},
onShow: function () {
},
onLoad: function (){
var that = this;
wx.getSystemInfo({
success(res) {
that.deviceWidth = res.windowWidth,
that.deviceHeight = res.windowHeight
}
})
let data = { path: '/pages/activity/groupActivityOne' }
this.api.getWXCode(data,(res)=>{
// 将图片临时保存到本地
wx.downloadFile({
url: res.data.data.url,
success (res) {
if (res.statusCode === 200) {
that.wxCode = res.tempFilePath // 小程序码图片
wx.downloadFile({
url: wx.getStorageSync('userInfo').avatarUrl,
success (res) {
if (res.statusCode === 200) {
that.headIcon = res.tempFilePath // 头像
wx.downloadFile({
url: 'https://nlwxapp.oss-cn-beijing.aliyuncs.com/static/butiful.png',
success (res) {
if (res.statusCode === 200) {
that.bgImgPath = res.tempFilePath// 背景图
}
}
})
}
}
})
}
}
})
})
},
created(){
if (wx.getStorageSync('userInfo')) {
this.userInfo = wx.getStorageSync('userInfo')
}
},
methods: {
circleImg(ctx, img, x, y, r){
ctx.save();
var d = 2 * r;
var cx = x + r;
var cy = y + r;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
// ctx.setStrokeStyle('rgba(0,0,0,0)')
canvas.stroke()
ctx.clip();
ctx.drawImage(img, x, y, d, d);
ctx.restore()
},
showImg() {
var that = this;
const ctx = wx.createCanvasContext('myCanvas'); // 设置背景
ctx.setFillStyle('#E72454')
ctx.fillRect(0,0,315,393) // 绘制图片
ctx.drawImage(that.bgImgPath, 0, 0, 315, 250)
// 绘制头像
that.circleImg(ctx,that.headIcon,315/2-50, 20, 50) //创建文字
ctx.setFontSize(18)
ctx.setFillStyle('#fff')
ctx.setTextAlign('center')
ctx.fillText(that.userInfo.nickName + '邀请你一起分奖金', 315 / 2, 140)
ctx.stroke() ctx.setFontSize(42)
ctx.setFillStyle('#FFD288')
ctx.textAlign = 'center'
ctx.fillText(that.message.money, 157, 180) var a = ctx.measureText(that.message.money).width
ctx.setFontSize(16)
ctx.setFillStyle('#FFD288')
ctx.fillText('元', 157 + 5 + a/2, 180)
ctx.stroke() // 绘制小程序码
ctx.drawImage(that.wxCode, 315/2-75, 200, 150, 150) context.setFontSize(12)
context.setFillStyle("#fff")
context.setTextAlign("center")
context.fillText("长按识别小程序", 157, 310)
ctx.stroke()
//渲染
ctx.draw() //需要把canvas转成图片后才能保存
wx.canvasToTempFilePath({ // 生成图片并把绘制的图片路径保存到一个变量
x: 0,
y: 0,
width: 315,
height: 393,
destWidth: 1260, //2倍关系
destHeight: 1572, //2倍关系
canvasId: 'myCanvas',
success: function (res) {
//关键 赋值给变量
that.shareImgSrc = res.tempFilePath
that.saveImg2()
},
fail: function (res) {
console.log(res)
}
})
},
saveImg2() {
var that = this
wx.saveImageToPhotosAlbum({ // 这张图片保存到本地相册
//shareImgSrc为canvas赋值的图片路径
filePath: that.shareImgSrc,
success(res) {
console.log('保存成功');
wx.showModal({
title: '保存成功',
content: '图片成功保存到相册了,快去发朋友圈吧~',
showCancel: false,
confirmText: '确认',
confirmColor: '#21e6c1',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
})
}
})
}
}
}
</script> <style>
canvas{
width: 315px;
height: 393px;
position: fixed;
left: 75rpx;
top: 50%;
margin-top: -435rpx;
}
/*控制canvas显示和隐藏 top值要大 要保证能溢出到屏幕外面*/
.topScroll{
position: absolute;
top: -2000rpx;
}
</style>
微信小程序生成分享图片,保存到本地的更多相关文章
- 微信小程序base64图片保存到手机相册
问题:base64图片不能直接用wx.saveImageToPhotosAlbum保存到手机相册 解决: 先用fs.writeFile写入本地文件,再wx.saveImageToPhotosAlbum ...
- 微信小程序图片保存到本地
微信小程序图片保存到本地是一个常用功能: 这里讲解下完整实现思路: 因为微信官方的授权只弹一次,用户拒绝后再次调用,就需要结合button组件的微信开放能力来调起,以下方案在微信各种授权中可参考. w ...
- js截图及绕过服务器图片保存至本地(html2canvas)
今天要分享的是用html2canvas根据自己的需求生成截图,并且修复html2canvas截图模糊,以及绕过服务器图片保存至本地. 只需要短短的几行代码,就能根据所需的dom截图,是不是很方便,但是 ...
- 微信小程序裁剪图片成圆形
代码地址如下:http://www.demodashi.com/demo/14453.html 前言 最近在开发小程序,产品经理提了一个需求,要求微信小程序换头像,用户剪裁图片必须是圆形,也在gith ...
- 微信小程序分享朋友圈的实现思路与解决办法
实现思路 那么既然小程序没有分享到朋友圈的api,我们怎么实现分享到朋友圈呢,下面我介绍一下实现思路. 既然没有捷径,那就走复杂一点的路线,那就是需要用户手动分享到朋友圈,问题又来了,用户手动分享的话 ...
- 微信小程序实现图片是上传、预览功能
本文实例讲述了微信小程序实现图片上传.删除和预览功能的方法,分享给大家供大家参考,具体如下: 这里主要介绍一下微信小程序的图片上传图片删除和图片预览 1.可以调用相机也可以从本地相册选择 2.本地实现 ...
- 微信小程序动态生成保存二维码
起源:最近小程序需要涉及到一些推广方面的功能,所以要写一个动态生成二维码用户进行下载分享,写完之后受益良多,特此来分享一下: 一.微信小程序动态生成保存二维码 wxml: <view class ...
- 微信小程序 分享海报
const app = getApp(); const template = require('../../template/templates.js'); Page({ /** * 页面的初始数据 ...
- golang-vue实现微信小程序分享到朋友圈
最近涉及到微信小程序分享到朋友圈,不知道微信为什么不直接接口分享,咱也不敢佛,咱也不敢问,只能百度问度娘,看官方文档,网上的一些分享五花八门,每一个重点的,所以整理了一下到底怎样生成二维码分享图片才是 ...
随机推荐
- WIN7不能被远程桌面问题
不知从何时起,我的机器不能被远程桌面.在其他机器远程我,最后都提示"凭据不工作",账号和密码肯定是正确的. 我是开了远程桌面的: 也许是新近开了防火墙的缘故?检查防火墙,3389是 ...
- JavaScript 实现块级作用域
(function(){ 块级作用域: })();
- 用WaveX实现音频文件的录音
原文地址:https://blog.csdn.net/gongluck93/article/details/53096013 1.WaveInOpen waveInOpen MMRESULT wave ...
- 汉字与区位码互转(天天使用Delphi的String存储的是内码,Windows记事本存储的文件也是内码),几个常见汉字的各种编码,utf8与unicode的编码在线查询,附有读书笔记 good
汉=BABA(内码)=-A0A0=2626(区位码)字=D7D6(内码)=-A0A0=5554(区位码) 各种编码查询表:http://bm.kdd.cc/ 汉(记住它,以后碰到内存里的数值,就会有敏 ...
- 解决VMware安装Ubuntu的过程中窗口过小无法看到【下一步】按钮的问题
只要按住ALT键向上拖动窗口
- Swift(一)简单值
Swift的源文件扩展名是.swift 按照国际惯例,学习一门新语言写的第一个程序都是在屏幕上输出一句 “Hello, world!” .在Swift里,一行代码就搞定了: 如果你以前写过C或者Obj ...
- silverlight中 ComboBox绑定数据库,并获取当前选定值
silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...
- Linux系统的方法论
Linux系统的方法论 https://www.cnblogs.com/youxia/p/LinuxDesktop001.html 阅读目录 特别说明 什么情况下适合玩Linux桌面 Linux桌面系 ...
- Code-NFine:变量修改
ylbtech-Code-NFine:Cookie变量修改 1. NFine.Code返回顶部 1.Operator 1.1.OperatorProvider.cs /**************** ...
- View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows
Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...