5 Replies Latest reply on Sep 22, 2009 10:03 AM by Flex harUI

    Is it possible to dynamically load an image/resources embed in a SWF?

    ppk luk

      Hi all,

       

         i have created a flex library project, just containing images (.GIF)...etc.

       

         And i create another flex application project that merge the .GIF files into the final SWF application.

       

        based on the current time, i want to display different pictures (e.g. sun.GIF, moon.GIF, star.GIF...etc..there are many images) in the application right-top corner.

       

        as, i know, <mx:image/> just work with static image reference for embedded resource.

      (<mx:image source="@Embed('sun.GIF'))"/>).

       

        i search by google, it seems that, for embedded image or resource, you can't load it dynamically.

       

        the following code don't work.

        e.g. var mmm:Image = new Image();
                     mmm.source = fileName;
                     mmm.load();

       

       

        anybody find the solution???

       

        thank you.

       

      ppk luk

        • 1. Re: Is it possible to dynamically load an image/resources embed in a SWF?
          sir_teddy

          Use the Loader() or SWFloader() to load stuff dynamically.

          • 2. Re: Is it possible to dynamically load an image/resources embed in a SWF?
            ppk luk Level 1

            Hi,

             

              i have tried this method before, but, again, it don't work for embedded resource. IO error will be thrown, with error message.

             

            IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2036: Load Never Completed. URL: file:///E:/Flex/TmpAssetApp/bin-debug/@Embed(source='images/sun.GIF')"]

             

            (the GIF is in a folder called images)

             

            SWFLoader/Loader can only load file dynamically from URL, but not embedded resource?? (by syntax @Embed(source='xxx'))??

             

            my sample code:

             

            =======================================

             

            <mx:Script>
                    <![CDATA[
                        import mx.controls.Alert;
                       
                       
                        [Bindable]
                        private var fileName:String = "@Embed(source='images/last.GIF')";
                       
                        /**
                         * Show the image
                         */
                       
                        private function showImage(event:flash.events.MouseEvent):void{
                           
                            fileName = fileTxt.text;
                           
                            dynaLoader.source = "@Embed(source='" + fileName + "')";
                            //dynaLoader.addEventListener(flash.events.Event.COMPLETE, ss);
                            //dynaLoader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, ff); 
                            dynaLoader.load();
                               
                        }
                       
                        private function ss(event:flash.events.Event):void{
                           
                                mx.controls.Alert.show("result1\n" + event);
                           
                        }
                       
                        private function ff(event:flash.events.IOErrorEvent):void{
                           
                                mx.controls.Alert.show("result2\n" + event);
                           
                        }
                       
                    ]]>
                </mx:Script>
                <mx:SWFLoader height="305" left="10" right="10" bottom="10" id="dynaLoader" autoLoad="true" source="{fileName}" ioError="ff(event)" complete="ss(event)"/>

             

            =======================================

             

               Do you know any other solution?

             

              thank you.

             

            ppk luk


            • 3. Re: Is it possible to dynamically load an image/resources embed in a SWF?
              Flex harUI Adobe Employee

              When you embed an image, you are copying all of the data of that image into the SWF, so by definition, it is not and cannot be dynamic.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Is it possible to dynamically load an image/resources embed in a SWF?
                ppk luk Level 1

                Hi,

                 

                  it is to my surprise that i can't dynamically loading image/resource in a SWF file.

                 

                  so can i conclude that, in flex, you can't dynamically load resource(image/xml...etc) which is situated

                within the SWF?

                 

                  and if i want to simulate the 'dynamically' loading effect based on user's input

                ....i need to write/generate clumsy code like the following?

                 

                  ================================================

                  e.g. i have 10 images: pic1.gif, pic2.gif......pic10.gif. in a SWF

                 

                  i have to write code:

                 

                  <mx:image id="pic1" source="pic1.gif" visible="false"/>

                  <mx:image id="pic2" source="pic1.gif" visible="false"/>

                   ....

                   ....

                   ....

                   <mx:image id="pic10" source="pic10.gif" visible="false"/>

                 

                 

                 

                   then in some event handling, do:

                 

                   switch(desiredPicString){

                      case "pic1":

                         pic1.visible = true;

                         break;

                      case "pic2":

                         pic1.visible = true;

                         break;

                      ....

                      ....

                      ....

                      case "pic10":

                         pic1.visible = true;

                         break;

                  }

                 

                  ================================================

                 

                  thank you.

                • 5. Re: Is it possible to dynamically load an image/resources embed in a SWF?
                  Flex harUI Adobe Employee

                  If you want to switch between Embeds at runtime, you assign the source to the variable that holds the embed.

                   

                  Your code would look like:

                   

                  [Embed(source="pic1.gif");

                  public var image1:Class;

                  [Embed(source="pic2.gif");

                  public var image2:Class;

                  [Embed(source="pic3.gif");

                  public var image3:Class;

                  [Embed(source="pic3.gif");

                  public var image3:Class;

                   

                  Then if you have:

                    <mx:image id="myImage" />

                   

                   

                  In your switch statement you just assign the variable:

                   

                     switch(desiredPicString){

                        case "pic1":

                            myImage.source = image1

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui