Dynamically loading images using the UILoader component in Flash using ActionScript 3.0

by Peter deHaan on December 18, 2008

in UILoader

The following example shows how you can dynamically load an external image into a Flash/ActionScript 3.0 UILoader container by setting the source property to a URL or calling the load() method and passing a URLRequest object.

Full code after the jump.

The following example auto loads an external image by setting the source property to an image URL:

// ActionScript 3.0
/* Requires:
 * - UILoader control in Library
 */
 
import fl.containers.UILoader;
 
var uiLoader:UILoader = new UILoader();
uiLoader.source = "http://helpexamples.com/flash/images/image1.jpg";
uiLoader.move(10, 10);
addChild(uiLoader);

The following example loads an external image using the load() method and a URLRequest object:

// ActionScript 3.0
/* Requires:
 * - UILoader control in Library
 */
 
import fl.containers.UILoader;
 
var urlReq:URLRequest = new URLRequest();
urlReq.url = "http://helpexamples.com/flash/images/image1.jpg";
 
var uiLoader:UILoader = new UILoader();
uiLoader.load(urlReq);
uiLoader.move(10, 10);
addChild(uiLoader);

{ 9 comments… read them below or add one }

1 Dada 12.18.08 at 5:24 pm

I think use Loader to load a image must be better,because Loader is more light than UILoader.
Loader is extends Loader DisplayObjectContainer InteractiveObject DisplayObject EventDispatcher Object
And UILoader is extends UILoader UIComponent Sprite DisplayObjectContainer InteractiveObject DisplayObject EventDispatcher Object
When we load thousands of images,loader will user less memory.:)

2 Bobby 01.12.09 at 11:37 am

hello,
i need to create 3 buttons. when clicked on any of them they take me to a jpg or sfw file using the uiloader technique. can someone tell me how to do this
thank u

3 crason 02.12.09 at 9:25 am

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var myTween1:Tween = new Tween(pic1_btn, “x”, Bounce.easeOut, -100, 1000, 2, true);
var myTween2:Tween = new Tween(pic2_btn, “x”, Bounce.easeOut, -100, 850, 2, true);
var myTween3:Tween = new Tween(pic3_btn, “x”, Bounce.easeOut, -100,700, 2, true);
var myTween4:Tween = new Tween(pic4_btn, “x”, Bounce.easeOut, 100, 400, 2, true);
var myTween5:Tween = new Tween(pic5_btn, “x”, Bounce.easeOut, 100, 250, 2, true);
var myTween6:Tween = new Tween(pic6_btn, “x”, Bounce.easeOut, 1000, 100, 2, true);
var myTween7:Tween = new Tween(pic7_btn, “x”, Bounce.easeOut, 1050, 550, 2, true);

//This tells the movie clips to act like buttons
pic1_btn.buttonMode = true;
pic2_btn.buttonMode = true;
pic3_btn.buttonMode = true;
pic4_btn.buttonMode = true;
pic5_btn.buttonMode = true;
pic6_btn.buttonMode = true;
pic7_btn.buttonMode = true;

function Pic1(e:MouseEvent):void
{
display.source = “pic1.swf”;
}

function Pic2(e:MouseEvent):void
{
display.source = “pic2.swf”;
}

function Pic3(e:MouseEvent):void
{
display.source = “pic3.swf”;
}
function Pic4(e:MouseEvent):void
{
display.source = “pic4.swf”;
}

function Pic5(e:MouseEvent):void
{
display.source = “pic5.swf”;
}

function Pic6(e:MouseEvent):void
{
display.source = “pic6.swf”;
}
function Pic7(e:MouseEvent):void
{
display.source = “pic7.swf”;
}

pic1_btn.addEventListener(MouseEvent.CLICK, Pic1);
pic2_btn.addEventListener(MouseEvent.CLICK, Pic2);
pic3_btn.addEventListener(MouseEvent.CLICK, Pic3);
pic4_btn.addEventListener(MouseEvent.CLICK, Pic4);
pic5_btn.addEventListener(MouseEvent.CLICK, Pic5);
pic6_btn.addEventListener(MouseEvent.CLICK, Pic6);
pic7_btn.addEventListener(MouseEvent.CLICK, Pic7);

4 Timo 05.13.09 at 12:29 pm

If I use the code to load my own images, they are loaded and resized to a very small picture. How can I change the size of this picture?

5 ian_banderaz 06.04.09 at 8:40 am

try this code timo
uiLoader.setSize(150,200);

6 Anonymous 06.17.09 at 2:30 pm

or u can add this:

loader.scaleContent = false;

it will risize the loader to the size of every image

7 Christian 07.22.09 at 9:26 pm

How can you load something that is already in the library?

8 Nuno 10.02.09 at 8:45 am

you can add a linkage name in the library such as MyObject and then just set uiLoader.source = MyObject .

9 Khalifa 12.26.09 at 11:43 am

How to unload the UILoader

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: