2 Replies Latest reply on Oct 2, 2009 6:38 AM by mewk

    Load Image, Source Name From XML File

    Snazzo

      I have an xml file that loads with a text  string value and a string value that contains the path and image file name. I can't seem to get this to display in the swf. I am attempting to get the file from disk and specify the name of the file in the xml.

       

      I get the Error #1065: Variable png is not defined or Error #1065: Variable @Embed(source='null') is not defined.

       

       

       

       

       

       

       

      private function resultHandler(event:ResultEvent):void

      {

       

       

      XMLData = event.result.bannerContent;

      bannerMainContent = XMLData.text1;

      bannerImage = XMLData.image;

       

      }

       

      <s:BitmapImage

       

      source="@Embed(source={bannerImag})" resizeMode="scale" id="bitmapimage1" />

       

      Any help?

        • 1. Re: Load Image, Source Name From XML File
          David_F57 Level 5

          You are trying to load images dynamically, embed is used to include the image in your swf file,

           

          so if xmldata.image = "images\mypic.jpg" your source is then myimage.source="{xmldata.image}"

           

          this is over simplified, you really need to load the image into the image component with a loadcomplete listener to ensure that the image is displayed otherwise the screen may be updated before the image is recieved resulting in no image being displayed.

           

          If you need additional help with this just let me know

           

          David.

          • 2. Re: Load Image, Source Name From XML File
            mewk Level 3

            Well, it's an interesting problem: how to embed assets from an external xml file. And I don't think it can be done, not with any normal practices that I've seen anyway!

             

            Certainly the way you've set it up is bound to run into trouble. Think about it, the embed meta tag is a directive to the compiler to incorporate the asset into the swf, while your result handler must necessarily be used after the creation of the swf (duh!) and even later than this because the swf must load and then receive an asynchronous return.

             

            I'm actually a little confused why the compiler didn't complain outright from the code as it was. Flex builds your app every time you click the save button and this is a case where an error should have been thrown. If you had tried something like this in the script block

            <script>
                  public var bannerImage:String = "myAsset.asset";
                 [Embed(source=bannerImage)] var bannerImage1:Class;
            </script>
            

            the compiler WOULD complain. It threw me off that your code actually compiled and so I tried to think of any alternative methods (like embedding your asset in a style sheet and then loading that through a module), but I came up short. Sorry! Maybe someone out there cleverer or more experienced than I has some brilliant workaround??

             

            Anyway, I don't know why you would need to embed assets this way and like David suggested you could and probably should just bind the source property to some variable rather than embedding.

             

            my two cents,

             

            - e