Flex application to load an image from URL

Sometimes we need to load an image into our Flex application from URL, it's very simple if the images are in a web-accessible directory. The below code is a sample for it. You can modify it to suit  your application.
 

// ActionScript file: LoadImg.as
package imgLib
{
 import flash.display.Loader;
 import flash.events.Event;
 import flash.net.URLRequest;
 
 import mx.core.UIComponent;

 public class LoadImg extends UIComponent
 {
  private const image_path:String = "
http://www.foxarc.com/blog/stuff/backgrounds/dream-01.jpg";
  private var loader:Loader;
  private var request:URLRequest;

  public function LoadImg()
  {
   loader=new Loader();
   request=new URLRequest(image_path);
   loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
   loader.load(request);

  }
  private function onComplete(event:Event):void {
   addChild(loader);
        }
 }
}

 

 

//mxml file: LoadURLImg.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onAppInit()" backgroundColor="#ffffff" xmlns:map="imgLib.*">
<mx:Script>
<![CDATA[
public function onAppInit():void{
Security.allowDomain("http://blog.foxarc.com/");
}
]]>
</mx:Script>
<map:LoadImg width ="100%" height="100%" />
</mx:Application>




Comment: 0 | Read times: -
Announce commentary
Your name
Content
Validation code Code