简体   繁体   English

Blob:无法解码URL上的图片

[英]Blob: Unable to decode image at URL

I'm currently making a Word Web Add-in. 我目前正在制作Word Web加载项。 This uses Internet Explorer as engine. 这使用Internet Explorer作为引擎。 My Add-in needs to load multiple selected images from the users computer. 我的加载项需要从用户计算机加载多个选定的图像。

Because some of the selected images might be quite big, I resize them using HTML5 canvas. 由于某些选定的图像可能很大,因此我使用HTML5 canvas调整了它们的大小。 This is my code to resize: 这是我要调整大小的代码:

    function makeSmallImage(imageContainer, retries)
    {
        if (retries === undefined)
            retries = 0;

        console.log('Resizing image..')
        return new Promise(function (resolve, reject)
        {
            img = img || new Image();

            img.onload = function ()
            {
                // calculate new size
                var width = 200;
                var height = Math.floor((width / img.naturalWidth) * img.naturalHeight);

                console.log('new size', width, height);

                try
                {
                    // create an off-screen canvas
                    canvas = canvas || document.createElement('canvas'),
                        ctx = ctx || canvas.getContext('2d');

                    // antialiasing
                    ctx.imageSmoothingEnabled = true;

                    // set its dimension to target size
                    canvas.width = width;
                    canvas.height = height;

                    // draw source image into the off-screen canvas:
                    ctx.drawImage(img, 0, 0, width, height);

                    // clean up
                    imageContainer.largeData = undefined;
                    if (img.src.substr(0, 4) === 'blob')
                        URL.revokeObjectURL(img.src);
                    img.src = '';

                    // encode image to data-uri with base64 version of compressed image
                    var newDataUri = canvas.toDataURL();
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    console.log('Image resized!');
                    imageContainer.resizedData = newDataUri;

                    resolve(imageContainer);
                }
                catch (e)
                {
                    if (img.src !== undefined && img.src.substr(0, 4) === 'blob')
                        URL.revokeObjectURL(img.src);

                    console.log(e);
                    if (e.message === "Unspecified error." && retries < 5)
                    {
                        setTimeout(function (imgContainer, re)
                        {
                            makeSmallImage(imgContainer, re).then(resolve).catch(reject);
                        }, 2000, imageContainer, retries + 1);
                    }
                    else
                        reject('There was an error while trying to resize one of the images!');
                }
            };

            try
            {
                var blob = new Blob([imageContainer.largeData]);
                img.src = URL.createObjectURL(blob);
            } catch (e)
            {
                reject(e);
            }
        });
}

'img', 'canvas' and 'ctx' are global variables, so the same elements are reused. “ img”,“ canvas”和“ ctx”是全局变量,因此可以重复使用相同的元素。 'imgcontainer.largedata' is an uint8array. 'imgcontainer.largedata'是一个uint8array。 To avoid a lot of memory usage i'm loading and resizing the images one by one. 为了避免占用大量内存,我正在逐一加载和调整图像大小。

Despite of that, after loading for example 120 images of 10mb, it might happen that I get an error: 尽管如此,例如在加载120张10mb的图像后,可能会发生错误:

Unable to decode image at URL: 'blob:D5EFA3E0-EDE2-47E8-A91E-EAEAD97324F6' 无法解码URL上的图片:“ blob:D5EFA3E0-EDE2-47E8-A91E-EAEAD97324F6”

I then get an exception "Unspecified error", with not a lot more info. 然后,我得到一个异常“未指定的错误”,没有更多的信息。 You can see in the code now that I added a litle mechanism to try again, but all new attempts fail. 您现在可以在代码中看到我添加了一个litle机制来重试,但是所有新尝试均失败。

I think the reason is that internet explorer is using too much memory. 我认为原因是Internet Explorer使用了过多的内存。 I think some resources are not being cleaned up correctly, but I can't seem to spot a memory leak in my code here (if you can, please let me know). 我认为某些资源没有被正确清理,但是我似乎无法在这里的代码中发现内存泄漏(如果可以的话,请告诉我)。

Does anybody have an idea of how I could fix this, or work around this? 有谁知道我该如何解决或解决这个问题?

if you try to resize the image, why not directly use the office APIs? 如果您尝试调整图像的大小,为什么不直接使用Office API? you can first get the images, then use the height/width property to resize it, such as 您可以先获取图像,然后使用height / width属性调整其大小,例如

image1.height = 5; image1.width = 5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM