<canvas canvas-id="canvas" style="width:{{cWidth}}px;height:{{cHeight}}px;position: absolute;left:-1000px;top:-1000px;"></canvas>
wx.chooseImage({
count: 9, // 默认9
sizeType: ['compressed'], // 指定只能为压缩图,首先进行一次默认压缩
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: photo => {
console.log(photo);
//将tempFilePaths[0]加入到images数组 作为展示
_this.data.images.push(photo.tempFilePaths[0])
_this.setData({
images: _this.data.images
})
//-----返回选定照片的本地文件路径列表,获取照片信息-----------
photo.tempFilePaths.forEach(item => {
console.log(item);
wx.getImageInfo({
src: item,
success: res => {
//---------利用canvas压缩图片--------------
var ratio = 2;
var canvasWidth = res.width //图片原始长宽
var canvasHeight = res.height
while (canvasWidth > 750 || canvasHeight > 1200) { // 保证宽在750,高1200
canvasWidth = Math.trunc(res.width / ratio)
canvasHeight = Math.trunc(res.height / ratio)
ratio++;
}
_this.setData({
cWidth: canvasWidth,
cHeight: canvasHeight
})
//----------绘制图形并取出图片路径--------------
var ctx = wx.createCanvasContext('canvas')
ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
ctx.draw(false, setTimeout(function () {
wx.canvasToTempFilePath({
canvasId: 'canvas',
destWidth: canvasWidth,
destHeight: canvasHeight,
success: res => {
let token = wx.getStorageSync('token') || ""
console.log(res.tempFilePath, '最终图片路径') //最终图片路径
let path = res.tempFilePath
wx.uploadFile({
url: '',//上传地址
method: 'POST',
header: {
"token": token
},
filePath: res.tempFilePath,
name: 'image',
success(res) {
console.log(res);
}
})
return
//base64 格式
let arr = path.split('.');
let list = {};
//获取图片类型
list.imageType = '.' + arr[arr.length - 1];
//对图片进行base64 获取base64Code
list.base64Code = wx
.getFileSystemManager()
.readFileSync(path, 'base64');
console.log(list, _this.data.images)
//将list加入到imageList数组中 作为后台接口参数
_this.data.imageList.push(list)
},
fail: res => {
console.log(res.errMsg)
}
})
}, 1000))
}, //留一定的时间绘制canvas
fail: res => {
console.log(res.errMsg)
}
})
})
}
})