Saving and Loading BitmapData

Hi to all,
I save a BitmapData as:
let dataUri = this.bmdDraw.canvas.toDataURL();
localStorage.setItem(this.prev_image_key, dataUri);

Then i try to load data this way.
My code:
let localDataUri = localStorage.getItem(this.image_key);
let data = document.getElementById(“temp_img”);
data[‘src’] = localDataUri;, localDataUri, data);

And I get a next error when adding a picture to the cache:
Phaser.Cache.AddImage: Image “randomKey” hasn’t been retrived yet

But the image is uploaded to the temp_img web element.
What am I doing wrong? How can I save and load BitmapData correctly?



Is it correct until this point ? So we can dig more

I realized my mistake, I had to use the event ‘onload’, and not immediately try to use the loaded data.
here is the working code:
let img: any = document.getElementById(“temp_img”); // or new Image()
img.onload = () => {, localDataUrl, img);
img[‘src’] = localDataUrl;