const handleShare = async (text) => { const context = Taro.createCanvasContext('canvas'); const qrCodePath = text; const qrCodeInfo = await Taro.getImageInfo({ src: qrCodePath }); context.drawImage(qrCodeInfo.path, 0, 0, qrCodeInfo.width, qrCodeInfo.height, 0, 0, 400, 400); context.draw(false, async () => { await Taro.canvasToTempFilePath({ canvasId: 'canvas', fileType: 'jpg', quality: 1, success: (res) => { Taro.showShareImageMenu({ path: res.tempFilePath, success() { console.log('分享成功'); }, fail(err) { console.log('分享失败', err); }, }); }, fail(err) { console.log('canvasToTempFilePath 失败', err); }, }); }); }; 报错:getImageInfo:fail file not found"}
时间: 2024-04-28 20:26:38 浏览: 149
Vue源码中要const _toStr = Object.prototype.toString的原因分析
如果你的图片是 base64 编码的,那么你需要在调用 `Taro.getImageInfo` 方法时,使用 `src` 参数传入 `base64` 数据,而不是图片的路径。例如:
```
const qrCodePath = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAt0lEQVR42mNkQALy/8fFgYGBj4+Pj/05OTkREREQGQABAwMD/AwP/gYGBgREREf+Pj4+MjIy/wMDAwf8DA/4MDAwGDAwMDIyMj/8DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwP+Pj4wMDAwf8MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA/wMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA//8DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAf4+PjAwMDA//8AAAD//wMAgAEAAgMBAAAIAAAACAAAAAgAAAAIAAAACAAAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAAAgAA/wAAAP//AAD//wAA//8AAP//AAD//wAA'
const qrCodeInfo = await Taro.getImageInfo({ src: qrCodePath });
```
另外需要注意的是,由于小程序的限制,`canvasToTempFilePath` 方法无法直接将 base64 图片转换成临时文件路径,因此你需要先将 base64 图片转换成 `wx.base64ToArrayBuffer` 类型的数据,再将它绘制到 canvas 上。你可以使用以下代码将 base64 图片转换成 `wx.base64ToArrayBuffer` 类型的数据:
```
const base64ToArrayBuffer = (base64) => {
const binaryString = atob(base64.split(',')[1]);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
};
const imageData = base64ToArrayBuffer(qrCodePath);
```
然后你可以将 `imageData` 传入 `Taro.canvasPutImageData` 方法,将它绘制到 canvas 上。最后再调用 `Taro.canvasToTempFilePath` 方法将 canvas 转换成临时文件路径即可。
阅读全文