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. POJ - 1753 Flip Game (IDA*)

    题意:4*4的棋盘摆满棋子,有黑有白,翻转一个棋子的同时也将翻转其上下左右的棋子(翻转后黑变白,白变黑),问使棋盘上所有棋子颜色相同,最少翻转的棋子数. 分析: 1.每个棋子至多翻转1次.翻转偶数次与 ...

  2. Django xadmin图片上传与缩略图处理

    基本摘要 用python django开发时,个人选中Xadmin后台管理系统框架,因为它*内置功能丰富, 不仅提供了基本的CRUD功能,还内置了丰富的插件功能.包括数据导出.书签.图表.数据添加向导 ...

  3. 静听网+python爬虫+多线程+多进程+构建IP代理池

    目标网站:静听网 网站url:http://www.audio699.com/ 目标文件:所有在线听的音频文件 附:我有个喜好就是听有声书,然而很多软件都是付费才能听,免费在线网站虽然能听,但是禁ip ...

  4. Unity 可重复随机数

    出处 https://blogs.unity3d.com/cn/2015/01/07/a-primer-on-repeatable-random-numbers/   (英文原版) http://ww ...

  5. BZOJ [Scoi2010]游戏

    题解: 解法一:建立图论模型,发现只要联通块中有环则这个联通块中的值都可以被攻击到 如果是树,则只能攻击size-1个 解法二:二分图匹配,二分答案,看看是否能攻击到mid #include<i ...

  6. 一个算法题--Self Crossing

    You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...

  7. A4纸网页打印

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Z0J 3772 Calculate the Function 线段树+矩阵

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...

  9. WinServer--mstsc

    部署项目时,远程连接服务器一直提示连接达最大拒绝连接. mstsc /admin 从sp2后,/console参数就改为/admin了,其实就是登陆到远程服务器的控制台,一般加这个参数是为了在远程桌面 ...

  10. Django2.0——实现简易登陆、注册

    思路: 实现简易的登陆.注册,我们至少需要三个HTML页面,一个主页面.一个登陆界面.一个注册界面.为了存储和校验用户的账号和密码,我们需要写一个模型类(用于映射到数据库).两个form类(一个登陆. ...