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. Ganglia 安装 No package 'ck' found

    安装ConcurrecyKit,下载地址:https://github.com/concurrencykit/ck   编译安装即可 下面是一个歪果仁的解决办法,不过我没用上 I was buildi ...

  2. filter滤镜兼容ie的rgba属性

    要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,0.1); 但是要兼容到ie8.这个就有点 ...

  3. 了解redis

    redis:非关系型数据库,基于内存高性能,key-value存储,一般用作缓存,开源的使用ANSI C语言编写,遵守BSD协议,支持网络,可基于内存亦可持久化的日志型.Key-Value数据库,并提 ...

  4. URL&HTTP协议详解

    本文来自公开课笔记,主要做知识的记录,谢谢! ·接口测试核心技术--URL&HTTP协议详解. ·URL: 统一资源定位符. 示例: https://ke.qq.com/course/3157 ...

  5. 数组 bash shell

    http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html #!/bin/bash # 脚本检测到离线主机会发送邮件通知,可一直运行. ...

  6. Oracle 基础1

    oracle基础 表空间: Oracle数据库对数据的管理是基于表空间的概念来的, 各种数据的以及存储数据的优化, 实际上也是通过优化表空间来实现的 表空间分类: 永久表空间 用来存放表的数据, 视图 ...

  7. WOJ 1543 Array

    还是聪哥给我讲的思路才知道的,起初我利用两两互质去求发现有问题,互质只是必要条件而非充分条件,后来还是用的标准思路 即其实最终只要保留最大的素数的幂即可,其他包含该素数幂但指数低的都不用了,这样就能保 ...

  8. 哈希表hashTable的Java设计

    1:哈希表的概念 2:设计原理 3:哈希表的Java设计

  9. DevOps云翼日志服务实践

    10月30日,全球权威数据调研机构IDC正式发布<IDCMarketScape:中国DevOps云市场2019,厂商评估>报告.京东云凭借丰富的场景和实践能力,以及高质量的服务交付和平台稳 ...

  10. 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...