微信小程序生成海报保存图片到相册小测试
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();
}
})
微信小程序生成海报保存图片到相册小测试的更多相关文章
- 小程序生成海报图片(或者原有的)并下载,&&相册授权&&按钮拉起二次授权
这是自己做小程序生成推广海报,并保存到本地相册的方法,向后台发起请求,返回一个海报图片,下载保存到相册, 如果只是单纯的下载图片代码43行-63行就足够了 如果想直接保存到相册,则不要做downFil ...
- 微信小程序生成海报分享:canvas绘制文字溢出如何换行
主要思路: 1.先分割为字符串数组,然后一个字一个字绘图,利用ctx.measureText(string) 方法,获取绘制后的宽度,判断宽度在多少内就另起一行,再将各行拼成一个字符串 2.计算另起的 ...
- 小程序生成海报 canvas
前言 微信小程序需要生成海报进行朋友圈分享,但是不同的手机会有问题, 然后首先是图片的问题 图片 在模拟器上没有报错,可是真机测试却什么也没画出来. canvas.drawImage 是不支持网络图片 ...
- 小程序生成海报:通过 json 配置方式轻松制作一张海报图
背景 由于我们无法将小程序直接分享到朋友圈,但分享到朋友圈的需求又很多,业界目前的做法是利用小程序的 Canvas 功能生成一张带有二维码的图片,然后引导用户下载图片到本地后再分享到朋友圈.而小程序 ...
- 小程序生成海报demo
效果图: <view class='poste_box' id='canvas-container' style="margin:0 auto;border-radius:16rpx; ...
- 微信小程序例子-保存图片到手机相册
微信小程序例子-保存图片到手机相册 1.关键代码 1)WXML文件 2)JS文件 saveImgToPhotosAlbumTap: function(){ // 图片必须是 https 的 var I ...
- 微信小程序保存图片到相册
先来看小程序中的保存图片到相册的api wx.saveImageToPhotosAlbum({ filePath : "./test.png", //这个只是测试路径,没有效果 s ...
- 微信小程序点击保存图片到相册
wxml部分的代码 <view class="footer-r" bindtap="save"> <image src="../ ...
- 微信小程序生成太阳码
微信小程序生成太阳码 https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token 必须通过POST提交 而且参数 ...
随机推荐
- POJ 1039:Pipe 计算几何
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9773 Accepted: 2984 Description ...
- Information:java: Errors occurred while compiling module 错误
在用 IDEA 启动 tomcat 时 发现项目编译报错,如图所示 于是安装网上的方法把 JDK 版本都改了一下 改完之后按照道理来说,应该编译通过的,但是我就想,编译不通过肯定跟 IDEA 的配置有 ...
- 如何通过C语言获取主机WLAN下的IPv4地址,MAC地址
#include "stdio.h" #include "windows.h" void GetHostWLAN_IPv4_AND_MAC(char IPv4[ ...
- python刷LeetCode:9. 回文数
难度等级:简单 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: fa ...
- [RoarCTF 2019]Simple Upload
0x00 知识点 1:Think PHP上传默认路径 默认上传路径是/home/index/upload 2:Think PHP upload()多文件上传 think PHP里的upload()函数 ...
- VS2013 MFC opencv 播放视频
看网上有很多人用的还是CvvImage类,但是Opencv3.0已经没有CvvImage这个类了.百度得之可以使用以前的类,稍作修改就可以了. 头文件: #pragma once #ifndef CV ...
- redis--主从复制(读写分离)
应用程序对服务器大量的读写,服务器很可能会宕机,导致数据丢失.为了解决这一问题就有了主从复制. 作用: 1:防止数据丢失 2:提高系统的吞吐量 主从复制:从服务器复制主服务器中的数据. 读写分离:应用 ...
- Python 时间 time
import time print time.strftime("%Y-%m-%d") import datetime print datetime.datetime.now() ...
- Intellij IDEA破解方法
1.破解(参考网站) http://idea.lanyus.com/ 2. Intellij idea使用教程 https://github.com/tengj/IntelliJ-IDEA-Tutor ...
- JS高级学习笔记(2)之js多线程
参考大神:Javascript多线程 web worker ---- 6.Web Worker 概述 截图过来: 线程之间的通信 let worker = new Worker(‘js文件路径’) 主 ...