框架类型 | 问题类型 | API/组件名称 | 终端类型 | 微信版本 | 基础库版本 |
---|---|---|---|---|---|
小程序 | Bug | wx.createCanvasContext | 微信安卓客户端 | 8.0.23 | 2.24.5 |
网友回复
Wait:
//压缩图片
compressImage() {
let that = this;
console.log(this.data.isUploadIndex,that.data.imgs)
let imagePath = that.data.imgs[this.data.isUploadIndex];
console.log("====================正在压缩第 " + this.data.isUploadIndex + " 张图片")
console.log("=========压缩前图片路径:" + imagePath)
//获取图片大小
wx.getFileInfo({
filePath: imagePath,
success(res) {
console.log("=========压缩前图片大小:", res.size / 1024, 'kb');
if (res.size > 1024 * 1024) {
//需要压缩
//获取图片信息
wx.getImageInfo({
src: imagePath,
success(imageInfo) {
var maxSide = Math.max(imageInfo.width, imageInfo.height);
//画板的宽高默认
var windowW = that.data.canvas_width;
var scale = 1;
if (maxSide > windowW) {
scale = windowW / maxSide;
}
var imageW = Math.floor(imageInfo.width * scale);
var imageH = Math.floor(imageInfo.height * scale);
that.setData({
canvas_height: imageH
})
//压缩图片
console.log("=========开始压缩")
var ctx = wx.createCanvasContext('photo_canvas');
ctx.drawImage(imagePath, 0, 0, imageW, imageH)
ctx.draw(false, function () {
wx.canvasToTempFilePath({
canvasId: 'photo_canvas',
fileType: "jpg",
success: function (res) {
console.log("=========压缩后图片路径:" + res.tempFilePath)
wx.getFileInfo({
filePath: res.tempFilePath,
success(res) {
console.log("=========压缩后图片大小:" + res.size / 1024 + 'kb');
}
})
that.uploadImage(res.tempFilePath)
}
}, this)
})
}
})
} else {
console.log("=========无需压缩")
that.uploadImage(imagePath)
}
}
})
},