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在这段函数中怎么增加一个删除缓存照片的功能
时间: 2024-01-18 18:02:28 浏览: 169
逐步除错-微信小程序调用wx.getimageinfo遇到的坑解决
您可以在 `wx.canvasToTempFilePath` 的 `success` 回调函数中,使用 `wx.saveFile` 将临时图片文件保存到本地,然后在需要删除缓存照片的时候,使用 `wx.getSavedFileList` 获取本地已保存的文件列表,遍历查找到对应的文件路径后,使用 `wx.removeSavedFile` 删除该文件即可。示例代码如下:
```javascript
wx.canvasToTempFilePath({
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
canvasId: 'photo_canvas',
success: function (res) {
var filePaths = res.tempFilePath;
wx.saveFile({
tempFilePath: filePaths,
success: function(res) {
var savedFilePath = res.savedFilePath;
// 在这里添加删除缓存照片的功能
wx.getSavedFileList({
success: function(res) {
res.fileList.forEach(function(file) {
if (file.filePath === savedFilePath) {
wx.removeSavedFile({
filePath: savedFilePath,
complete: function(res) {
console.log('缓存照片已删除');
}
});
}
});
}
});
}
});
}
});
```
阅读全文