I'm trying to display images with URL.createObjectURL
. However, it takes so much memory, about 10 times of image size.
Here is my code:
var image = new Image();
image.src = URL.createObjectURL(blob);
image.className = 'images';
image.onload = function(){
URL.revokeObjectURL(this.src);
};
$('.images').appendTo('body');
Is this a browser bug? Or something's wrong with my code?
The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.
createObjectURL is a static method provided by the URL Web API. Returns a DOMString containing a unique blob URL, that is a URL with blob: as its scheme, followed by an opaque string uniquely identifying the object in the browser.
createObjectURL is synchronous but it seems to complete almost instantaneously. Also note that calling URL. revokeObjectURL in the image's onload handler breaks "Open image in New Tab" in the image's context menu.
Actually you call URL.revokeObjectURL() when you release an existing object URL which was previously created by calling window.URL.createObjectURL() for optimal performance and memory usage, if there are safe times when you can explicitly unload them.
But you can't save memory through creating object, read this answer, and this helper link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With