const handleShare = async (text) => { const context = Taro.createCanvasContext('canvas'); const qrCodeBase64 = text; const qrCodeInfo = await Taro.getImageInfo({ src: `data:image/png;base64,${qrCodeBase64}` }); 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-01-23 22:03:44 浏览: 138
这个错误通常是因为`Taro.getImageInfo`方法无法识别base64编码的图片,导致无法正确获取到图片信息。可以尝试将`qrCodeBase64`转换为本地图片路径,然后再调用`Taro.getImageInfo`方法获取图片信息。你可以尝试修改代码如下:
```
const handleShare = async (text) => {
const context = Taro.createCanvasContext('canvas');
const qrCodeBase64 = text;
const qrCodePath = Taro.env === "WEB" ? `data:image/png;base64,${qrCodeBase64}` : qrCodeBase64; // 判断是否为Web端,如果是则拼接base64编码,否则直接使用路径
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);
},
});
});
}
```
这样修改后,可以先判断当前是否为Web端,如果是则拼接base64编码,否则直接使用路径。然后再调用`Taro.getImageInfo`方法获取图片信息。希望能帮到你。
阅读全文