Internet Resource

Three ways to load image files in Flex

The following source code shows three ways that how to load an image file.

  1. <?xml version=1.0 encoding=utf-8?>
  2. <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout=absolute applicationComplete=InitApp()>
  3. <mx:Script>
  4. <![CDATA[ 
  5.  
  6. //the first way:it will build “image.jpg” file into swf ( after building into swf,the image.jpg file is NOT needed)
  7. [Bindable] 
  8. [Embed(source=image.jpg)] 
  9. private var imgClass:Class;
  10. //the second way
  11. private var _loader:Loader;
  12.  
  13. private function InitApp():void{ 
  14.  
  15. // source code of the first way
  16. _img.source = imgClass;
  17.  
  18. //source code of the second way
  19. _loader = new Loader();
  20. //notice: NOT _loader.addEventListener,is  _loader.contentLoaderInfo.addEventListener
  21. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{ 
  22. _img.source = e.currentTarget.content;
  23. });
  24. _loader.load(new URLRequest(encodeURI(image.jpg)));
  25.  
  26. //the third way
  27. _img.source = image.jpg//Notice: set img autoLoad’s property to true
  28.  
  29. //image.jpg file is needed by the second and the third way’s *.swf
  30. } 
  31.  
  32. ]]>
  33. </mx:Script>
  34. <mx:Image x=”51″ y=”62″ width=”298″ height=”245″ autoLoad=”true” id=”_img”/>
  35. </mx:Application>

1 Comment

  1. Why do I get a security sandbox violation when trying to load an external image?

    SecurityError: Error #2122: Security sandbox violation: LoaderInfo.content

Comments are closed.