2 Replies Latest reply on Mar 21, 2008 8:29 PM by ericgla

    addChild and Loader

    ericgla
      I'm rather new to Flex, moving over from the world of .NET. I am a bit confused on loading an external image using the Loader class. I created the following class:

      public class imgView extends UIComponent
      {
      private const image_path:String = "{image url here}";
      private var loader:Loader;
      private var request:URLRequest;

      public function imgView()
      {
      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);
      }
      }

      In the onComplete event, the Loader instance is added to the imgView instance which inherites from UIComponent. This code works and I am able to see the image. I'm guessing that addChild is smart enough to look at the data contained in the loader, and determine that it's an image based on the mime type, and/or the image header. Is my assumption correct?

      Also, this UIComponent only seems to display correctly when it's the only control added to the state. Otherwise I just see a blank screen. Is there additional work I need to do to get my UI component to play nicely with the other controls?

      Any insight would be greatly appreciated!

      --Eric
        • 1. Re: addChild and Loader
          peterent Level 2
          First of all, why can't you use mx.controls.Image which would have taken most of the problems go away.

          PS: addChild() doesn't know anything - it just adds the child to the display list of the component. You also don't have to wait for the complete event to add the loader to the UIComponent.
          • 2. Re: addChild and Loader
            ericgla Level 1
            I should have mentioned that my Flex app is talking to an ASP.NET web service, and I need to set an http header value in order to get ASP.NET Session to work on the server side. If there is a way to get a reference to the Image control underlying loader, that would be great!

            --Eric