How to load an image into our Flex application from URL(with source)

 

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 only a simple sample for it.You can modify it to suit your app.
PS.
While working on an AS 3 script I was getting an error like below(image):
Severity Description Resource In Folder Location Creation Time Id
2 Could not resolve <map:imgView> to a component implementation. imgView.mxml imgView 2008/02/10 1:17:08 26

I checked my action script file but there was no syntax error.Well was only because I forgot to create the “myLib” folder!

Download: imgView.as

  1. // ActionScript file (imgView.as)
  2. package myLib
  3. {
  4. import flash.display.Loader;
  5. import flash.events.Event;
  6. import flash.net.URLRequest;
  7. import mx.core.UIComponent;
  8. public class imgView extends UIComponent
  9. {
  10. private const image_path:String = “<a href=”http://blog.minidx.com/wp-content/uploads/2007/12/is.jpg”>http://blog.minidx.com/wp-content/uploads/2007/12/is.jpg”;</a>
  11. private var loader:Loader;
  12. private var request:URLRequest;
  13. public function imgView()
  14. {
  15. loader=new Loader();
  16. request=new URLRequest(image_path);
  17. loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
  18. loader.load(request);
  19. }
  20. private function onComplete(event:Event):void {
  21. addChild(loader);
  22. }
  23. }
  24. }

Download: imgView.mxml

  1. //mxml file(imgView.mxml):
  2. <?xml version=”1.0″ encoding=”utf-8″?>
  3. <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”onAppInit()” backgroundColor=”#3f3f3f” xmlns:map=”myLib.*”>
  4. <mx:Script>
  5. <![CDATA[
  6. public function onAppInit():void{
  7. Security.allowDomain(“http://blog.minidx.com/”);
  8. }
  9. ]]>
  10. </mx:Script>
  11. <map:imgView width =”100%” height=”100%” />
  12. </mx:Application>