Friday, April 17, 2009

Loading image using Loader.loadBytes()

Loader class can load image from ByteArray. I used this method before a year ago to set a part of a screen for Screen Sharing application. Right now I was having a case where I need to create an image from Base64 encoded string. I am using following code to do this ::

var str:String = ""; //It won't be an empty string :) because it will a base64 encoded string of image.

var byteArr:ByteArray = Base64.converToByteArray(str); //Here you can use any ready made library to decode and convert the String into ByteArray. I used ready made library from here.

var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);

loader.loadBytes(byteArr);

function onImageLoaded(e:Event):void{

addChild(loader);

trace(loader.width + " : " + loader.height);

}


That's it. This can be useful where you can not load the image from server because you don't have the image but it's data as string. May this can be useful to some one :)

No comments: