源码:http://115.28.152.1:3000/zhouyan/picture.git
图片压缩 1网络图片需要调用downloadFile 获取在本地的路径--》drawCanvas
2本地图片 直接调用 drawCanvas
// 图片压缩
pictap: function () {
var that = this;
var url ='https://mdapi.zjwist.com/mediainfo3/get/67587';
wx.downloadFile({
url: url,
success: function (sres) {
console.log(sres);
url = sres.tempFilePath;
that.drawCanvas(url);
}, fail: function (fres) {
}
})
},
//利用Canvas 缩放图片
drawCanvas: function (url) {
var that = this;
var ctx = wx.createCanvasContext('attendCanvasId');
ctx.drawImage(url, 0, 0, 100, 100);
ctx.draw(true, function () {
that.prodImageOpt(url); });//在回调进行保存不会出现空白
},
// 生成图片
prodImageOpt: function (url) {
console.log(url);
var that = this;
wx.canvasToTempFilePath({
canvasId: 'attendCanvasId',
success: function success(res) {
var path = res.tempFilePath;
console.log(path);
that.setData({
canvasImgUrl: path
});
wx.setStorageSync("path", path);
}
});
}
.xml 文件代码
<canvas style=" width: 100px;
height: 100px;
" canvas-id="attendCanvasId"/>