我试图通过多个步骤调整图像大小以提高最终图像的质量。这在Opera浏览器中运行良好,但我认为Firefox中存在内存问题。调整几个图像的大小后,出现以下错误:错误:图像损坏或截断:blob:[XY]。所以我的问题是:您是否在以下代码中看到了内存问题?
loadImage.halfSize = function (i) {
var c = document.createElement("canvas");
c.width = i.width / 2;
c.height = i.height / 2;
var ctx = c.getContext("2d");
ctx.drawImage(i, 0, 0, c.width, c.height);
return c;
};
loadImage.renderImageToCanvas = function (
canvas,
img,
sourceX,
sourceY,
sourceWidth,
sourceHeight,
destX,
destY,
destWidth,
destHeight
) {
canvas.width = sourceWidth;
canvas.height = sourceHeight;
canvas.getContext('2d').drawImage(
img,
sourceX,
sourceY,
sourceWidth,
sourceHeight);
if(sourceWidth <= destWidth && sourceHeight <= destHeight) {
return canvas;
}
while (canvas.width > destWidth * 2) {
canvas = loadImage.halfSize (canvas);
}
var canvasRes = document.createElement("canvas");
canvasRes.width = destWidth;
canvasRes.height = destHeight;
var canvasResCtx = canvasRes.getContext("2d");
canvasResCtx.drawImage(canvas, 0, 0, canvasRes.width, canvasRes.height);
return canvasRes;
};
我无法看到泄漏,因为我在任何地方使用局部变量,因此每次调用halfSize()后都应释放创建的canvas元素的内存?
感谢您的帮助! 本