test.wxml

<canvas style="width:{{imageWidth}}px;height:{{imageHeight}}px;" canvas-id="myCanvas" class='canvas' hidden='{{hideme}}'></canvas>
<image wx:if="{{tempPath!=''}}" class="viewimg" src="{{tempPath}}" mode="widthFix"></image>
<view class="operbtns">
<button class="button" type="primary" bindtap="savePic">保存图片</button>
</view>

test.js

Page({

  data: {
title: '测试',
tempPath: '',
hideme: false
}, onLoad: function (options) {
var that = this;
var imageSize = that.image();
that.setData({
imageWidth: imageSize.imageWidth,
imageHeight: imageSize.imageHeight,
windowscale: imageSize.windowscale,
}); //获取背景图片
that.getBackground();
//获取头像
//that.getAvatar(); wx.showToast({
title: '正在生成图片',
icon: 'loading',
duration: 10000,
});
}, image: function() {
var imageSize = {};
var originalScale = 1;//图片高宽比
//获取屏幕宽高
wx.getSystemInfo({
success: function (res) {
var windowWidth = res.windowWidth;
var windowHeight = res.windowHeight;
var windowscale = windowHeight / windowWidth;//屏幕高宽比
imageSize.windowscale = windowscale;
if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比
//图片缩放后的宽为屏幕宽
imageSize.imageWidth = windowWidth;
imageSize.imageHeight = Math.floor(windowWidth * 1.779);
} else {//图片高宽比大于屏幕高宽比
//图片缩放后的高为屏幕高
imageSize.imageHeight = windowHeight;
imageSize.imageWidth = Math.floor(windowHeight / 1.779);
} }
})
return imageSize;
}, getBackground: function () {
var that = this;
wx.downloadFile({
url: 'https://a.b.c/public/images/bg.png',
success: function (res) {
that.setData({
tempPath: res.tempFilePath,
share: res.tempFilePath,
})
//把背景画到画布上
that.createImg();
},
fail: function () {
console.log('fail')
}
})
}, createImg: function () {
var that = this;
var ctx = wx.createCanvasContext('myCanvas');
ctx.setFillStyle('White');
ctx.fillRect(0, 0, that.data.imageWidth, that.data.imageHeight);
ctx.drawImage(that.data.share, 0, 0, that.data.imageWidth, that.data.imageHeight);
ctx.save();
//ctx.beginPath();
//ctx.arc(that.data.imageWidth / 2, that.data.imageHeight * 0.63, 65, 0, 2 * Math.PI);
//ctx.clip();
//ctx.drawImage(that.data.avatar, that.data.imageWidth / 2 - 60, that.data.imageHeight * 0.55, 120, 120);
//ctx.restore();
ctx.setTextAlign('center');
ctx.setFillStyle('#ffffff');
ctx.setFontSize(22);
ctx.fillText(that.data.title, that.data.imageWidth / 2, that.data.imageHeight * 0.41); ctx.draw();
//显示新画的图片
that.viewPic();
}, viewPic: function(){
var that = this;
wx.canvasToTempFilePath({
width: that.data.imageWidth,
height: that.data.imageHeight,
canvasId: 'myCanvas',
success: function (res) {
that.setData({
tempPath: res.tempFilePath,
hideme: true
});
}
});
wx.hideToast();
}, savePic: function () {
var that = this;
wx.showToast({
title: '正在保存图片',
icon: 'loading',
duration: 10000,
});
util.savePicToAlbum(that.data.tempPath);
}, onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onShareAppMessage: function () {
},
onPullDownRefresh: function () {
wx.stopPullDownRefresh();
}
})

微信小程序生成海报保存图片到相册小测试的更多相关文章

  1. 小程序生成海报图片(或者原有的)并下载,&&相册授权&&按钮拉起二次授权

    这是自己做小程序生成推广海报,并保存到本地相册的方法,向后台发起请求,返回一个海报图片,下载保存到相册, 如果只是单纯的下载图片代码43行-63行就足够了 如果想直接保存到相册,则不要做downFil ...

  2. 微信小程序生成海报分享:canvas绘制文字溢出如何换行

    主要思路: 1.先分割为字符串数组,然后一个字一个字绘图,利用ctx.measureText(string) 方法,获取绘制后的宽度,判断宽度在多少内就另起一行,再将各行拼成一个字符串 2.计算另起的 ...

  3. 小程序生成海报 canvas

    前言 微信小程序需要生成海报进行朋友圈分享,但是不同的手机会有问题, 然后首先是图片的问题 图片 在模拟器上没有报错,可是真机测试却什么也没画出来. canvas.drawImage 是不支持网络图片 ...

  4. 小程序生成海报:通过 json 配置方式轻松制作一张海报图

    背景 由于我们无法将小程序直接分享到朋友圈,但分享到朋友圈的需求又很多,业界目前的做法是利用小程序的 Canvas 功能生成一张带有二维码的图片,然后引导用户下载图片到本地后再分享到朋友圈.而小程序 ...

  5. 小程序生成海报demo

    效果图: <view class='poste_box' id='canvas-container' style="margin:0 auto;border-radius:16rpx; ...

  6. 微信小程序例子-保存图片到手机相册

    微信小程序例子-保存图片到手机相册 1.关键代码 1)WXML文件 2)JS文件 saveImgToPhotosAlbumTap: function(){ // 图片必须是 https 的 var I ...

  7. 微信小程序保存图片到相册

    先来看小程序中的保存图片到相册的api wx.saveImageToPhotosAlbum({ filePath : "./test.png", //这个只是测试路径,没有效果 s ...

  8. 微信小程序点击保存图片到相册

    wxml部分的代码   <view class="footer-r" bindtap="save"> <image src="../ ...

  9. 微信小程序生成太阳码

    微信小程序生成太阳码 https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token 必须通过POST提交 而且参数 ...

随机推荐

  1. python_生成器小结

    #__author : "ziChuan" #__data : 2019/7/19 import random # print(random.random()) # print(r ...

  2. TX2安装pycharm&tensorflow

    https://blog.csdn.net/zt1091574181/article/details/88899668 TX2 (JetPack4.2)安装 Pycharm&TensorFlo ...

  3. c++ STD Gems07

    reverse.rotate.permutation #include <iostream> #include <vector> #include <string> ...

  4. C/C++学习笔记_gdb调试

    1.前提条件:可执行文件包含调试信息 gcc -g 2.gdb 文件名 ---启动gdb调试 3.查看代码的命令 当前文件: list 行号(函数名) 指定文件: list 文件名:行号(函数名)4. ...

  5. 在Linux下 MySQL错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 解决办法【很管用】

    一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...

  6. 一天一个设计模式——Abstract Factory抽象工厂模式

    一.模式说明 前面学习了工厂方法(Factory Method)模式.在工厂方法模式中,在工厂方法模式中,父类决定如何生成实例,但并不决定所要生成的具体类,具体的处理交由子类来处理.这里学习的抽象工厂 ...

  7. 使用AJAX(阿贾克斯)创建级联菜单

    1. html页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  8. C++实现顺序表的14种操作

    C++顺序表的操作 2017-12-27 // 顺序表.cpp: 定义控制台应用程序的入口点. //Author:kgvito YinZongYao //Date: 2017.12.27 #inclu ...

  9. linux-文件系统和目录

    linux目录说明 /:系统根目录 /usr:用户的程序 /home:默认创建用户在此目录下创建用户主目录 /etc:存放系统配置文件.服务脚本,一些程序配置文件 /bin:常用命令 /sbin:常用 ...

  10. P(Y=y|x;θ)表示什么意思

    https://blog.csdn.net/jh1137921986/article/details/88999539 在机器学习中,特别是学习到关于概率/似然估计方面的内容,经常看到类似P(Y=y| ...