Setting img.src to dataUrl Leaks Memory

后端 未结 5 898
萌比男神i
萌比男神i 2021-01-02 04:16

Below I\'ve created a simple test case that shows that when an img tag\'s src is set to different dataUrls, it leaks memory. It looks like the image data is never unloaded a

5条回答
  •  别那么骄傲
    2021-01-02 05:04

    Setting the source to a fixed minimal dataURI after handling the image seems to fix the issue for me:

    const dummyPng = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';
    
    img.onload = () => {
        // ... process the image
        URL.revokeObjectURL(img.src);
        img.onload = null;
        img.src = dummyPng;
    };
    img.src = URL.createObjectURL(new window.Blob([new Uint8Array(data)], {type: 'image/png'}));
    

提交回复
热议问题