From f58a525f47fa5178af087d5bafbc5c6dca541aaa Mon Sep 17 00:00:00 2001 From: Antoine Vandecreme Date: Thu, 5 Nov 2015 22:16:20 -0500 Subject: [PATCH] Discard the image as soon as possible. --- src/imagetilesource.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/imagetilesource.js b/src/imagetilesource.js index b058dfc0..a0650be2 100644 --- a/src/imagetilesource.js +++ b/src/imagetilesource.js @@ -103,7 +103,7 @@ * @throws {Error} */ getImageInfo: function (url) { - var image = new Image(); + var image = this.image = new Image(); var _this = this; if (this.crossOriginPolicy) { @@ -122,7 +122,7 @@ _this._tileHeight = _this.height; _this.tileOverlap = 0; _this.minLevel = 0; - _this.levels = _this._buildLevels(image); + _this.levels = _this._buildLevels(); _this.maxLevel = _this.levels.length - 1; _this.ready = true; @@ -226,30 +226,34 @@ * @private Build the differents levels of the pyramid if possible * (canvas API enabled and no canvas tainting issue) */ - _buildLevels: function (image) { + _buildLevels: function () { var levels = [{ - url: image.src, - width: image.naturalWidth, - height: image.naturalHeight + url: this.image.src, + width: this.image.naturalWidth, + height: this.image.naturalHeight }]; if (!this.buildPyramid || !$.supportsCanvas || !this.useCanvas) { + // We don't need the image anymore. Allows it to be GC. + delete this.image; return levels; } - var currentWidth = image.naturalWidth; - var currentHeight = image.naturalHeight; + var currentWidth = this.image.naturalWidth; + var currentHeight = this.image.naturalHeight; var bigCanvas = document.createElement("canvas"); var bigContext = bigCanvas.getContext("2d"); bigCanvas.width = currentWidth; bigCanvas.height = currentHeight; - bigContext.drawImage(image, 0, 0, currentWidth, currentHeight); + bigContext.drawImage(this.image, 0, 0, currentWidth, currentHeight); // We cache the context of the highest level because the browser // is a lot faster at downsampling something it already has // downsampled before. levels[0].context2D = bigContext; + // We don't need the image anymore. Allows it to be GC. + delete this.image; if ($.isCanvasTainted(bigCanvas)) { // If the canvas is tainted, we can't compute the pyramid.