Apache – Programmatically load images in Flex

apache-flex

I need to load several images as Bitmap or BitmapData objects. The images are hosted outside the Flex project, and they're referenced by an external configuration file, so I can't embed them. Because the images won't be displayed directly to the user (they're being added to a PDF that is generated for download), creating a grouping of Image objects, attaching them to the application, and waiting for their LoadComplete handler to fire seems inefficient.

What is the best way to load these images into an application?

Best Answer

Have you considered using Loader class?

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest("image.png"));

function onLoad(e:Event):void
{
    var image:Bitmap = Bitmap(LoaderInfo(e.target).content);
    var bmpData:BitmapData = image.bitmapData;
    //use bmpdata the way you want
    trace(bmpdata.width);
    trace(bmpdata.height);
}
Related Topic