0 Replies Latest reply on Nov 16, 2010 2:48 AM by GuidoJos

    How to make a component wait for an event?

    GuidoJos

      Hey there Adobe community, I recently started using Flash Builder and I have a problem. I have a windowed application in Flash Builder with some script code and then a base64 image.

       

      <?xml version="1.0" encoding="utf-8"?>
      
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                             xmlns:s="library://ns.adobe.com/flex/spark" 
                             xmlns:mx="library://ns.adobe.com/flex/mx"
                             xmlns:flexlib="http://code.google.com/p/flexlib/" preinitialize="loadImage()">
          <fx:Script><![CDATA[
              import flash.display.BitmapData;
          import flash.utils.ByteArray;
      
          import mx.controls.Image;
          import mx.core.UIComponent;
          import mx.graphics.codec.JPEGEncoder;
          import mx.utils.Base64Encoder;
      
          private var image:Bitmap;
          private var base64image:String="";
      
          private function loadImage():void
          {
              var loader:Loader = new Loader;
              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
              loader.load(new URLRequest("mypic.jpg"));
              trace("started loading");
          }
          private function getimg():String
          {
              trace("flexlib accessing image ", base64image);
              return base64image;
          }
          private function imageLoaded(event:Event):void
          {
              image = new Bitmap(event.target.content.bitmapData);
              base64image = getBase64FromComponent(image);
              trace("image converted");
          }
      
      
          ]]></fx:Script>
      
          <flexlib:Base64Image value="{getimg()}"/>
      </s:WindowedApplication>
      
      
      

       

      The flexlib tag should render an image from a string. I want to load an image in the actionscript code, convert it to a string and then draw it. However, as soon as the application starts it attempts to render the string, which is still empty.

      The trace is like this, for some reason the getimg() is called twice:

      flexlib accessing image
      started loading

      flexlib accessing image
      image converted

       

      As you can see, the image isn't converted until after flexlib tries to draw it. The solution would be to make the flexlib component wait for the imageLoaded event, or make the specific tag re-initialize itself. How can I do this?