我正在尝试编写一个故障安全程序,该程序使用画布绘制非常大的图像(60 MB 可能是上限,而 10 MB 是下限)。很早以前就发现调用canvas的同步函数通常会导致页面在浏览器中崩溃,所以为了跨浏览器的兼容性toDataURL,我对程序进行了调整,使用了使用填充符的方法。toBlob我的问题是:使用该URL.createObjectURL(blob)方法的 Blob URL 能持续多久?
我想知道是否有一种方法可以缓存 Blob URL,以使其在浏览器会话之后持续存在,以防有人想在某一时刻渲染图像的一部分,关闭浏览器,然后回来完成它再次将 Blob URL 读取到画布中并从它停止的点恢复。我注意到这个可选的 autoRevoke 参数可能是我正在寻找的,但我想确认我正在尝试做的事情实际上是可能的。除非它涉及不同的解决方案,否则您的答案中不需要代码示例,如果可以使用此方法或其他方法使 Blob URL 在会话之外持续存在,我只需要一个是或否。(如果由于某种原因页面崩溃并且它也像“恢复会话”选项一样,这也会很方便。)
我在想这样的事情:
function saveCache() {
  var canvas = $("#canvas")[0];
  canvas.toBlob(function (blob) {
    /*if I understand correctly, this prevents it from unloading
      automatically after one asynchronous callback*/
    var blobURL = URL.createObjectURL(blob, {autoRevoke: false});
    localStorage.setItem("cache", blobURL);
  });
}
//assume that this might be a new browser session
function loadCache() {
  var url = localStorage.getItem("cache");
  if(typeof url=="string") {
    var img = new Image();
    img.onload = function () {
      $("#canvas")[0].getContext("2d").drawImage(img, 0, 0);
      //clear cache since it takes up a LOT unused of memory
      URL.revokeObjectURL(url);
      //remove reference to deleted cache
      localStorage.removeItem("cache");
      init(true); //cache successfully loaded, resume where it left off
    };
    img.onprogress = function (e) {
      //update progress bar
    };
    img.onerror = loadFailed; //notify user of failure
    img.src = url;
  } else {
    init(false); //nothing was cached, so start normally
  }
}
请注意,我不确定这是否会按照我的意图进行,因此任何确认都会很棒。
编辑刚刚意识到这sessionStorage与localStorage:P不同