Load an image from local storage in phaser 2/CE

Hello,
Is it possible to load an image in phaser 2 from browser local storage?
supposing that we have saved the image using this command in localstorage

localStorage.setItem('myImage', 'base64 encoded string representing the PNG image');

how can I load it in phaser2? I tried this code but it does not work:

game.cache.addImage('img', null, localStorage.getItem('myImage'));
var image = game.add.image(0, 0, 'img');


Thanks in advance.

I think you have to load it into cache first try

var loader = this.load.image("img",  localStorage.getItem('myImage'));
loader.addOnce("onLoadComplete", function(){
   var image = game.add.image(0, 0, 'img');
});

or do the whole this.load.image("img", localStorage.getItem('myImage')); on the preload state of the current state, then on the create function you should be able to load it normally with the img key.

Thanks for response, but it does not work. As a reminder in localStorage we have base64 encoded contents of image (that has been taken from camera of cell phone using cordova) not URL of image.

Try like that:

let image = new Image()
image.src = localStorage.getItem('myImage')

game.cache.addImage('img', null, image)

1 Like

maybe this will help http://jsfiddle.net/danthewolfe/6ja1kw32/ base64 needs slightly different syntax

2 Likes

Thanks you very much artvinn. you code works perfect. I was working on that for 2 days with no chance.

1 Like

your second suggestion is completely correct too netgfx. thanks

1 Like