Tips:three ways to load an image file in Flex

Similar to a previous article How to load an image into our Flex application from URL(with source),the following source code shows three ways that how to load an image file.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="InitApp()">
<mx:Script>
<![CDATA[ 

//the first way:it will build "image.jpg" file into swf ( after building into swf,the image.jpg file is NOT needed)
[Bindable]
[Embed(source="image.jpg")]
private var imgClass:Class;
//the second way
private var _loader:Loader; 

private function InitApp():void{ 

// source code of the first way
_img.source = imgClass; 

//source code of the second way
_loader = new Loader();
//notice: NOT _loader.addEventListener,is  _loader.contentLoaderInfo.addEventListener
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
_img.source = e.currentTarget.content;
});
_loader.load(new URLRequest(encodeURI("image.jpg"))); 

//the third way
_img.source = "image.jpg";  //Notice: set img autoLoad's property to true 

//image.jpg file is needed by the second and the third way's *.swf
} 

]]>
</mx:Script>
<mx:Image x="51" y="62" width="298" height="245" autoLoad="true" id="_img"/>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

6 Responses to “Tips:three ways to load an image file in Flex”

  1. Ajay says:

    I am using Flex Builder 3 with Rails to develop a web based application. I want to load an Pdf file into browser, i tried out some techniques but it’s not working.
    If you have any solution to this please let me know.

    thanks

  2. minidxer says:

    hi,Ajay
    one component in flex 1.5 called flash paper which allows you
    to render pdfs in flex ,but I don’t know whether is it available with Flex Builder 3.

  3. Carsten says:

    THANK YOU! This loader thing drove me crazy for more than an hour. thx for this post. even if its old.

    Carsten

  4. Tahir Alvi says:

    Hi i want to assign an image to a bit mapdata.
    How i do that?

  5. [...] introduced three ways to load an image file in Flex in previous article. Normally there are no any problem with loading progress of external files. You [...]

Leave a Reply