小程序使用wx.canvasToTempFilePath传入jpeg的图片就会报错:Uncaught (in promise) undefined
jpg和png的图片测试了没问题。这个压缩的太n了,整了一天都没整出来.
fileList.map((item, idx) => {
compressList[idx] = new Promise((resolve, reject) => {
wx.createSelectorQuery().select('#imageCanvas').fields({
node: true,
size: true
}).exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d')
const image = canvas.createImage()
image.src = item.url
image.onload = () => {
let ratio = 2
let canvasWidth = image.width
let canvasHeight = image.height
while (canvasWidth > 500 && canvasHeight > 500) {
canvasWidth = Math.trunc(res.width / ratio)
canvasHeight = Math.trunc(res.height / ratio)
ratio++
}
canvas.width = canvasWidth
canvas.height= canvasHeight
ctx.fillRect(0, 0, canvasWidth, canvasHeight)
ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight)
wx.canvasToTempFilePath({
canvas,
quality: 0.5,
fileType: 'jpg',
width: canvasWidth / 2, //canvas的宽高(生成图片的范围)
heght: canvasHeight / 2,
destWidth: (canvasWidth / 2) * wx.getWindowInfo().pixelRatio,
destHeight: (canvasHeight / 2) * wx.getWindowInfo().pixelRatio,
success: (res) => {
console.log(res);
resolve(res.tempFilePath)
},
fail: (res) => {
reject()
}
})
}
})
}).then(result => result)
})