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 09:02:28 浏览: 44
要删除这个画布产生的临时图片,你可以使用wx.getFileSystemManager()获取文件系统管理器,然后使用wx.getFileSystemManager().unlink()方法来删除临时图片。具体代码如下:
```
wx.getFileSystemManager().unlink({
filePath: filePaths,
success: function(res) {
console.log('成功删除临时图片');
},
fail: function(err) {
console.log('删除临时图片失败:', err);
}
})
```
相关问题
getCanvasImg: function (index, failNum, tempFilePaths) { let that = this; if (index < tempFilePaths.length) { wx.getImageInfo({ src: tempFilePaths[index], 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) // 将宽高大小设置到页面的canvas元素(checkCanvas)上 const ctx = wx.createCanvasContext('photo_canvas'); ctx.drawImage(tempFilePaths[index], 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 console.log(filePaths) that.data.pics || (that.data.pics = []) that.data.pics.push(...filePaths) that.setData({ pics: that.data.pics }) } }) }) } }) } },报错信息是:[渲染层网络层错误] Failed to load local image resource /pages/myProduct/h the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error) (env: macOS,mp,1.06.2303220; lib: 2.32.0)
这个错误信息提示你在加载本地图片资源时出现了问题,可能是由于服务器内部错误导致的。建议你检查一下本地图片资源的路径是否正确,并确认服务器是否正常运行。同时,可以检查一下调用 wx.getImageInfo()方法时传入的 tempFilePaths[index] 参数是否正确,并确保该图片资源存在。如果问题仍然存在,你可以尝试重新启动服务器或联系服务器管理员寻求帮助。
创建一个canvas元素(id为checkCanvas),获取对应的上下文对象,并设置图片宽高最大不能超过120,代码如下:const SIZE = 120 const ratio = Math.max(imgInfo.width / SIZE, imgInfo.height / SIZE, 1) const smallWidth = Math.trunc(imgInfo.width / ratio) const smallHeight = Math.trunc(imgInfo.height / ratio) // 将宽高大小设置到页面的canvas元素(checkCanvas)上 this.smallWidth = smallWidth this.smallHeight = smallHeight const ctx = wx.createCanvasContext('checkCanvas') ctx.drawImage(imgInfo.path, 0, 0, smallWidth, smallHeight) ctx.draw(false, function () { setTimeout(() => { wx.canvasToTempFilePath({ canvasId: 'checkCanvas', width: smallWidth, height: smallHeight, destWidth: smallWidth, destHeight: smallHeight, success: (res) => { resolve(res.tempFilePath) }, fail: (e) => { reject(e) } }) }, 100) }),怎么按照上面的步骤结合 addPic: function () { wx.chooseImage({ sizeType: ['compressed'], success: (imgRes) => { wx.getImageInfo({ src: imgRes.tempFilePaths[0], success: async (imgInfo) => { callBack && callBack(imgInfo); }, fail: () => {}, }); }, fail: () => {}, }) }进行操作
好的,我可以帮你将这段代码添加到addPic函数中。你需要在addPic函数中添加以下代码:
```
addPic: function (callBack) {
wx.chooseImage({
sizeType: ['compressed'],
success: (imgRes) => {
wx.getImageInfo({
src: imgRes.tempFilePaths[0],
success: async (imgInfo) => {
const SIZE = 120
const ratio = Math.max(imgInfo.width / SIZE, imgInfo.height / SIZE, 1)
const smallWidth = Math.trunc(imgInfo.width / ratio)
const smallHeight = Math.trunc(imgInfo.height / ratio)
const ctx = wx.createCanvasContext('checkCanvas')
ctx.drawImage(imgInfo.path, 0, 0, smallWidth, smallHeight)
ctx.draw(false, function () {
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId: 'checkCanvas',
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
success: (res) => {
callBack && callBack(res.tempFilePath)
},
fail: (e) => {
console.log(e)
}
})
}, 100)
})
},
fail: () => {},
});
},
fail: () => {},
})
}
```
这样,当你调用addPic函数时,它将会选择一张图片,然后将图片压缩至宽高最大不能超过120,并将其绘制到名为“checkCanvas”的canvas元素上,最后将canvas元素转换为临时文件路径并返回给回调函数。
阅读全文
相关推荐


