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. jQuery下拉框联动(JQ遍历&JQ中DOM操作)

    1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...

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

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

  3. Spring Boot Actuator Endpoints

    常用内建的Endpoints: beans:显示当前Spring应用上下文的Spring Bean完整列表(包含所有ApplicationContext的层次) conditions:显示当前应用所有 ...

  4. springboot-jar

    预览 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  5. A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题

    ---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...

  6. java 微信红包算法代码实现及架构设计

    转载至:https://www.zybuluo.com/yulin718/note/93148 微信红包的架构设计简介 架构 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈 ...

  7. scala def方法时等号和括号使用说明笔记

    scala定义方法时会指定入参和返回类型(无返回类型时对应Unit,即java和C中的void模式). 1.有入参,有返回类型时,scala具有类型推导功能,以下两种表达方式效果一样.但根据scala ...

  8. C++ STD Gems02

    remove.remove_if.replace.replace_if.remove_copy_if.unique #include <iostream> #include <str ...

  9. Git 小课堂 002——别名

    昨天我们聊了聊 Git 的文件存储,今天我们聊聊 Git 的别名.不知道你是不是熟悉别名,如果你经常使用命令行做一些事情,有一些复杂的命令,或者是一些简单的操作,往往用一些别名方法就很方便很容易,下面 ...

  10. 程序中出现list assignment index out of range的解决方法

    class stack: def __init__(self): self.num = 0 self.elem=[] def isEmoty(self): if self.num == 0: prin ...