ios 报错canvasToTempFilePath:fail invalid viewId?
wx.getImageInfo({
src: uri,
success(res) {
console.log(res)
// 创建离屏 2D canvas 实例
const canvas = wx.createOffscreenCanvas({ type: '2d', width: data.data?.w, height: data.data?.h })
const id = canvas.id
// 获取 context。注意这里必须要与创建时的 type 一致
const context = canvas.getContext('2d')
context.clearRect(0, 0, data.data?.w, data.data?.h)
// 创建一个图片
const image = canvas.createImage()
image.src = res.path // 要加载的图片 url
image.onload = () => {
console.log(canvas)
console.log(21)
context.drawImage(image, data.data?.x, data.data?.y, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height)
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: canvas.width,
height: canvas.height,
canvas: canvas,
canvasId: id,
success(res) {
console.log("res.tempFilePath = " + res.tempFilePath)
if (res.tempFilePath) {
callback && callback({
code: 0,
data: {
uri: res.tempFilePath,
width: canvas.width,
height: canvas.height
}
});
} else {
callback && callback({
code: 405,
msg: '照片处理失败,请重新选择或拍照!'
});
}
},
fail(err) {
console.log('fail', err);
callback && callback({
code: 405,
msg: '照片处理失败,请重新选择或拍照!'
});
}
})
}
},
fail(err) {
console.log('fail', err);
callback && callback({
code: 405,
msg: '照片处理失败,请重新选择或拍照!'
});
}
});