compressImage(index) {
let that = this
let item = this.files[index]
uni.getImageInfo({
src: item.path,
success(res) {
var ratio = 2;
var canvasWidth = res.width
var canvasHeight = res.height
while (canvasWidth > 1024 || canvasHeight > 1024) {
canvasWidth = Math.trunc(res.width / ratio)
canvasHeight = Math.trunc(res.height / ratio)
ratio++;
}
that.cWidth = canvasWidth
that.cHeight = canvasHeight
var ctx = uni.createCanvasContext('canvas')
console.log(0.3 * (res.width + res.height))
ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
ctx.draw(false, () => {
setTimeout(() => {
uni.canvasToTempFilePath({
canvasId: 'canvas',
fileType: 'jpg',
quality: 0.4,
success: function(res1) {
that.imgs.push(res1.tempFilePath)
if (index < that.files.length - 1) that.compressImage(
index + 1)
else uni.hideLoading()
},
fail: function(res) {
that.compressImage(index)
},
complete: function(res) {}
})
}, 0.3 * (res.width + res.height))
})
},
fail(err) {
that.compressImage(index)
}
})
}

已根据压缩前图片长款设定延迟执行时间 偶尔会出现图片绘制不全部分空白的情况发生 H5及模拟器皆会出现该问题 蹲一个解答