wxml 代码:

<view class="result-page">
<canvas bindtap="previewImage" canvas-id='myPicCanvas' style='width:100%;height:600px;margin: 0;padding: 0;display: block;background-color:#eeeeee'></canvas>
<button bindtap="savePic">保存图片</button>
</view>

js代码:

/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.drawCanvasPic(); },
previewImage: function () {
console.log(1);
wx.canvasToTempFilePath({
canvasId: 'myPicCanvas',
success: function (res) {
var tempFilePath = res.tempFilePath;
console.log(tempFilePath);
wx.previewImage({
current: tempFilePath, // 当前显示图片的http链接
urls: [tempFilePath] // 需要预览的图片http链接列表
})
},
fail: function (res) {
console.log(res);
}
}); },
drawCanvasPic: function () {
let ctx = wx.createCanvasContext('myPicCanvas');
let ctxW = 100;
let ctxH = 700;
// 默认像素比 // 屏幕系数比,以设计稿375*667(iphone7)为例
let XS = 375; /* 绘制头像 */
let avatarurl_width = 100; //绘制的头像宽度
let avatarurl_heigth = 100; //绘制的头像高度
let avatarurl_x = 50; //绘制的头像在画布上的位置
let avatarurl_y = 50; //绘制的头像在画布上的位置
ctx.save(); ctx.beginPath(); //开始绘制
//先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
ctx.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math.PI * 2, false); ctx.clip();//画好了圆 剪切 原始画布中剪切任意形状和尺寸。一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内 这也是我们要save上下文的原因 ctx.drawImage('https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2369419058,1797305489&fm=27&gp=0.jpg', avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); // 推进去图片,必须是https图片 ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 还可以继续绘制 ctx.draw(); //可将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中
},
savePic: function (e) {
console.log(111);
wx.canvasToTempFilePath({
canvasId: 'myPicCanvas',
success: function (res) {
console.log(res);
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(result) {
wx.showToast({
title: '图片保存成功',
icon: 'success',
duration: 2000
})
}
})
}
})
},

小程序 canvas实现图片预览,图片保存的更多相关文章

  1. 微信小程序:点击预览图片

    在开发微信小程序时,开发人员会参考着小程序api来开发小程序,但有的时候根据情况不同很容易出现bug,以下是我在开发小程序时出现的各种bug,在开发时有需要预览图片. 1.xml <view c ...

  2. previewImage.js图片预览缩放保存插件

    previewImage.js好用的图片预览缩放保存插件

  3. Wx-小程序-图片预览、保存

    点击图片预览 长按图片保存 点击按钮保存到手机相册 view: <!--wxml--> <text>点击图片预览.长按保存图片.点击按钮保存到系统相册</text> ...

  4. 小程序canvas生成二维码图片踩的坑

    1:生成临时图片,保证画布被加载以及渲染(即本身不可以 hidden 或是 上级元素不可以 hidden 或是 wx:if 隐藏等) == > 建议:因为 canvas 的组件层级(z-inde ...

  5. 微信小程序:点击预览大图功能

    点击预览大图功能 1. 给轮播图swiper-item绑定点击事件 2. 预览功能的本质是调用了小程序的api:previewImage 微信公众号----文档----开发----API----媒体- ...

  6. 微信小程序(组件demo)以及预览方法:(小程序交流群:604788754)

    1. 获取微信小程序的 AppID 登录 https://mp.weixin.qq.com ,就可以在网站的"设置"-"开发者设置"中,查看到微信小程序的 Ap ...

  7. 解决微信小程序wepy真机预览跟本地表现不一样,数据变化了视图没变化

    当时搜了很多相关问题都没找到相似的 只看到有这个相似的描述wepy在onLoad里修改data-object的值页面不渲染 ,通过setData解决的. 但是这个还不是根本的解决办法,有些地方用set ...

  8. 【微信小程序】下载并预览文档——pdf、word、excel等多种类型

    .wxml文件 <view data-url="https://XXX/upload/zang." data-type="excel" catchtap= ...

  9. 小程序图片预览 wx.previewImage

      list: [ 'http://img5.imgtn.bdimg.com/it/u=3300305952,1328708913&fm=26&gp=0.jpg', 'http://i ...

  10. JS实现图片base64转blob对象,压缩图片,预览图片,图片旋转到正确角度

    base64转blob对象 /** 将base64转换为文件对象 * @param {String} base64 base64字符串 * */ var convertBase64ToBlob = f ...

随机推荐

  1. 实习培训——Java多线程(9)

    实习培训——Java多线程(9) 很适合新手 http://www.cnblogs.com/GarfieldEr007/p/5746362.html http://www.cnblogs.com/Ga ...

  2. Marathon自动扩缩容(marathon-lb-autoscale)

    我们在服务里面创建如下的应用(以下是创建完复制过来的json): { "id": "/nginxtest", "cmd": null, &q ...

  3. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  4. 使用Pyinstaller打包python py文件注意事项

    首先需要将cmd或者PowerShell目录cd 跳转到py脚本所在目录,然后直接执行 Pyinstaller -F即可完成程序的打包,其中我们关心它有哪些常见参数: 输入参数的含义 -F 表示生成单 ...

  5. jenkins 常见问题汇总

    1.jenkins api调用 https://my.oschina.net/sanpeterguo/blog/197931 其中,有个错误的地方,带参数构建时,使用的时POST方法,如下: curl ...

  6. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. NetBeans issues and solutions.(build.xml and debug multiple projects)

    Copy a directory to another directory when building the .jar in NetBeans in the build.xml file. Solu ...

  8. cocos2dx 3.x(游戏中的储存-UserDefault)

      引擎提供的UserDefault可以简单地储存游戏中的信息,例如背景音乐音效的开关状态,用户名等等. UserDefault是一个单例对象,可以用getInstance方法来获得. 如果是第一次调 ...

  9. 多表关联查询_resultMap_集合对象

    多表关联查询_resultMap_集合对象_N+1方式实现 package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo ...

  10. Mybatis时间段比较

    在开始时间和结束时间内的一段时间范围的查询 <if test="timeStart != null and timeStart != ''"> and wfsj > ...