我在我的React函数中有以下代码,它从我的useEffect钩子中调用,我从API中获取base64格式的图像数据。基本上,控制台日志中显示***“正在绘制图像......”*从未出现在控制台日志中。我甚至看不到image. onerror报告的任何错误。我使用的是React 18.2.0。有人遇到过这个吗?我还注意到,设置base64编码JPEG内容的最后一条语句image.src()也失败了,并显示错误Error:找不到模块“数据:图像/jpeg;base64,undefined '**My UseEffect()大胆地正确获取数据,因为我可以在隐藏的img HTML元素中看到图像。
const handleDraw = (imageData) => {
console.log("handleDraw was called");
const img = document.getElementById("cardImage");
const canvas = canvasRef.current;
const context = canvas.getContext("2d");
var factor = Math.min ( canvas.width / img.width, canvas.height / img.height );
const image = new Image();
image.onload = function() {
console.log("Drawing image....handleDraw was called card content is ", card.content);
//context.scale(factor, factor);
context.drawImage(image, 0, 0, img.width-10, img.height-20);
//context.scale(1 / factor, 1 / factor);
context.font = "30px Arial";
context.fillStyle = "red";
context.fillText(message, 50, 50);
};
image.onerror = function(err) {
console.log("err", err);
};
const imgUrl = `data:image/jpeg;base64,${card.content}`
image.src = require(imgUrl);
};
1条答案
按热度按时间s5a0g9ez1#
变量card没有被定义。2这就是为什么你它没有被定义。3请定义card。4或者你可以简单的把card.content替换成imageData。5就像下面这样应该可以工作了。
常量图像URL =
data:image/jpeg;base64,${imageData}