Loading external images in AS3

| | Comments (7) | TrackBacks (0)
I thought I would post this as all the tutorials I found about loading external images in AS3 were either not working or very confusing. So here is a quick way of loading external images. For the sake of this example I will show only the methods needed in a class. First, we need to create a new Loader object. We then need to add an event listener to check when the image has loaded completely and add it to the display list. So lets start with creating the new object and adding the event.

	public function loadImage():void
	{
		var loader:Loader = new Loader;
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
		loader.load(new URLRequest("assets/sample.jpg"));
	}

Next we need to create a method to deal with the event that will be dispatched when the image has completely loaded. This method takes one parameter which is an object of the Event class. It contains the data that we want, in order to display this image on the stage. Remember drawing items on the stage is a 2 part process in AS3, create and add to stage.

	private function imageLoaded(event:Event):void
	{
		var image:Bitmap = new Bitmap(event.target.content.bitmapData);
		addChild(image);
	}

I hope this helps some of you out there who have been in the process of migrating from AS2 to AS3, or just been playing around. Remember to import the necessary classes to deal with the event, loader and bitmap objects we used. Selah.

0 TrackBacks

Listed below are links to blogs that reference this entry: Loading external images in AS3.

TrackBack URL for this entry: http://www.wezside.co.za/cgi-bin/mt/mt-tb.cgi/29

7 Comments

By luke on January 5, 2009 2:08 AM

yeah good code. waaaay simpler than the others out there.

By Insignia on March 19, 2009 2:58 AM

I thought it was going to be a long night but you proved me wrong. new UrlRequest is the code I needed in order to load images locally from my machine as opposed to an loading imagines from a url.

By flacoloco on March 23, 2009 9:36 PM

Thanks a lot. I loaded the image properly but I didn't use the Bitmap so I couldn't set the size of the image.

Great code!

By Sven on May 28, 2009 10:42 AM

And how do you use this code in a repeating image gallery with crossfading images?

I somehow need to get the image over and over again, but without calling the Loader a second time, as I asume the image is cached.

By Sven on May 28, 2009 1:34 PM

Well that's the problem, "almost instantly". The preloader shows up in a flash even when the image is cached.

I'd like an intelligent Loader, that only shows the preloader when the image is not cached.

By Sven on May 28, 2009 3:19 PM

In my experience the progress event fires whenever an image loads, whether from the local harddrive (cache) or the web. Which also seem logical as you actually call the event.

By worked on June 29, 2009 12:36 AM

Hey there- Thanks for the tip. Question. Once the image is loaded, how do I change the registration point (with actionscript)? I know how to manipulate a reg point on items found in the library. Any help would be appreciated. Thanks!

Leave a comment


Type the characters you see in the picture above.