wx.getImageInfo({ src: tempFilePaths, success: function (res) { const SIZE = 300 const ratio = Math.max((res.width/SIZE)*1, (res.height/SIZE)*1, 1) const smallWidth = Math.trunc(res.width / ratio) const smallHeight = Math.trunc(res.height / ratio) console.log('宽度:',smallWidth) console.log('高度:',smallHeight) console.log('缩放比例:',ratio) const ctx = wx.createCanvasContext('photo_canvas'); ctx.drawImage(tempFilePaths, 0, 0, smallWidth, smallHeight); ctx.draw(false, function () { wx.canvasToTempFilePath({ width: smallWidth, height: smallHeight, destWidth: smallWidth, destHeight: smallHeight, canvasId: 'photo_canvas', success: function (res) { var filePaths = res.tempFilePath wx.getFileSystemManager().unlink({ filePath: res.tempFilePath, success: function(res) { console.log('删除临时图片成功') // 清空画布 const ctx = wx.createCanvasContext('photo_canvas'); ctx.clearRect(0, 0, canvasWidth, canvasHeight) }, fail: function(res) { console.log('删除临时图片失败', res) } })这段代码中删除临时图片是失败的为什么?
时间: 2024-02-10 11:05:20 浏览: 97
根据你提供的代码,可能是因为在删除临时图片之前,清空画布的代码有误。在清空画布时,你使用了未定义的 `canvasWidth` 和 `canvasHeight` 变量。你需要先定义这两个变量,或者直接使用 `smallWidth` 和 `smallHeight` 变量来清空画布。
修改后的代码如下:
```
wx.getImageInfo({
src: tempFilePaths,
success: function (res) {
const SIZE = 300
const ratio = Math.max((res.width/SIZE)*1, (res.height/SIZE)*1, 1)
const smallWidth = Math.trunc(res.width / ratio)
const smallHeight = Math.trunc(res.height / ratio)
console.log('宽度:',smallWidth)
console.log('高度:',smallHeight)
console.log('缩放比例:',ratio)
const ctx = wx.createCanvasContext('photo_canvas');
ctx.drawImage(tempFilePaths, 0, 0, smallWidth, smallHeight);
ctx.draw(false, function () {
wx.canvasToTempFilePath({
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
canvasId: 'photo_canvas',
success: function (res) {
var filePaths = res.tempFilePath
wx.getFileSystemManager().unlink({
filePath: res.tempFilePath,
success: function(res) {
console.log('删除临时图片成功')
// 清空画布
const ctx = wx.createCanvasContext('photo_canvas');
ctx.clearRect(0, 0, smallWidth, smallHeight)
},
fail: function(res) {
console.log('删除临时图片失败', res)
}
})
}
})
})
}
})
```
请注意,如果删除临时图片的问题还未解决,你可以在控制台输出 `res` 对象来查看具体的错误信息,以便更好地定位问题。
阅读全文