Loading external images with ActionScript 3
One technique of loading external images is using the Loader class. It's similar to the URLLoader class but the Loader class loads and displays loaded images and SWF content. The Loader class is a part of the DisplayObject class; it is capable of displaying content as well as loading it. The goal of this technique is to make the loading operation as fast and hassle free as the queuing process of Getminted and other websites of similar nature.
To load external images using the Loader class, you need to create an object of the Loader class.
var imgLoader:Loader = new Loader();
To load an image into the Loader Object, the load method of the Loader class is used, along with a URLRequest.
var imgLoader:Loader = new Loader(); imgLoader.load(new URLRequest('image.jpg'));
The LoaderInfo class also has events and event types that dispatch information about the progress of the load through the Loader class.
The code below traces a loaded message after the loader object has completed loading the image.
var imgLoader:Loader = new Loader(); imgLoader.contentLoaderInfo.addEventListner(Event.COMPLETE,loaderCompleteHandler); function loaderCompleteHandler(e:Event):void{ trace('Image has loaded.'); } imgLoader.load(new URLRequest('image.jpg'));
At this point all you have done is loaded in the image to the Loader Object. For the loaded image to display you will need to add the Loader object to the display list.
addChild(imgLoader);
The complete source code looks like this.
var imgLoader:Loader = new Loader(); imgLoader.contentLoaderInfo.addEventListner(Event.COMPLETE,loaderCompleteHandler); function loaderCompleteHandler(e:Event):void{ trace('Image has loaded.'); addChild(imgLoader); } imgLoader.load(new URLRequest('image.jpg'));
hope this helps...
Do you need a working .fla of this including a preloader? Buy me a coffee and I'll send it to you via email. Remember to reference your email address.

Thank you for this! After about 30 google searches I finally found an easy way to do it, all the other tutorials were huge, about loading xml, server scripts, etc, etc. I just wanted an image in my file lol!
Anyways, thanks
I feel your pain Neticule. That’s the exact reason why I posted this article.
Thank you for your work. But it is some problem in using your code.
When I compile it then I recive the next error:
ReferenceError: Error #1069:… addEventListner в flash.display.LoaderInfo;
please help me?
Hi Serge,
There is a typo in the code. You need to change all the instances of addEventListner to addEventListener.
Hope this helps.. If your still having trouble let me know and I’ll build the actual loader for you so you can download it.
The code snippet below basically asks how you might check if a group of images has finished loading. Any help would be appreciated.
imageA.load(new URLRequest(“http://www.example.com/someimage_1.jpg”));
imageB.load(new URLRequest(“http://www.example.com/someimage_2.jpg”));
//etc
//make sure the image files are loaded
//but how to do it without chaining functions called on successively completed events
imageA.contentLoaderInfo.addEventListener( Event.COMPLETE, nextImgLoad);
function nextImgLoad (e:Event):void {
imageB.contentLoaderInfo.addEventListener( Event.COMPLETE, imageAnim);
removeEventListener(Event.COMPLETE, nextImgLoad);
//etc
}
Hi Groggles,
I apologize for the late reply. Just got back from a well deserved holiday.
Do you still need help with this? Or have you worked it out?
Thanks for the response. I never did work it out. Just left an event listener on the last image. I’m assuming that using an array might be part of the answer. Anyhow, any help would be appreciated. Cheers.
@Groggles
There are a few ways you could achieve this and yes one of them is using an array.
For example lets say you were reading the images from an xml file you would do the following.
Hope that makes sense…
Complete sense. That’s really very helpful. I have integrated it into my code and it works as expected. any thanks.
[Many]
Would you be so kind to add the code that allows repositioning?
The code works great, but I need to position the image.
Thank you.
Never mind. Figured it out.
imgLoader.x=-100;
imgLoader.y=-36;
Hi Pharos,
I was writing the response while you figured it out. Here it is any way..
var imgLoader:Loader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preloaderHandler);
function loaderCompleteHandler(e:Event):void{
trace(‘Image has loaded.’);
//create movieclip that will hold the img
var imageHolder:MovieClip = new MovieClip();
//Add the holder to the stage
addChild(imageHolder);
//Save the loaded bitmap to a local variable
var image:Bitmap = (Bitmap)(e.target.content);
// add the image to the movieclip
imageHolder.addChild(image);
//position the holder to where you want
imageHolder.x = stage.width/2;
imageHolder.y = 50;
}
function preloaderHandler(e:ProgressEvent):void{
trace(‘bytesLoaded: ‘ + e.bytesLoaded);
trace(‘bytesTotal: ‘ + e.bytesTotal);
trace(‘percentage loaded: ‘ +
Math.round((e.bytesLoaded/e.bytesTotal)*100) + “%”);
}
imgLoader.load(new URLRequest(‘http://www.flashnutz.com/FN.jpg‘));
This was a very good description of the Loader class. Thank you. NOw my question is, how do you loop this code to outputting multiple images from an array? I can do it with shapes and such but for some reason this loader class seems to get in the way of allowing me to output more than just the last image in my array loop. I think it’s making me crazy…
Thanks you very much!
I am find to long time this codes! Wonderful working, thank again!
I opine that to receive the home loans from creditors you must have a good reason. However, once I have received a collateral loan, just because I was willing to buy a building.