6 Replies Latest reply on Jan 14, 2010 1:22 PM by Francisc

    Embed images programatically

    Francisc Level 3

      Hello!

       

      Can embedding be done through code?

      [Embed(source="images/up.png")]

      private static const _up:Class;

       

       

      And how can I use such variables in this kind of context:

       

      var name:String="up';

      this["_"+up] etc

       

      Thank you!

        • 1. Re: Embed images programatically
          Claudino Bur Level 2

          Hello,

           

          Yes, embeding can be done in that way too. You could use it like

          var test:Bitmap = new _up();

          • 2. Re: Embed images programatically
            Francisc Level 3

            No, I was asking if I can embed in another way then the one I wrote.

            -and-

            If I can somehow concatenate embedded vars somehow.

            • 3. Re: Embed images programatically
              Jason Szeto Level 3

              Asset embedding is performed at compile time. The assets are added into the compiled swf. So there is no way to programmatically embed content in Flex. Instead, you'd have to do some preprocessing on your .mxml and .as files before they get compiled.

               

              Or just load them dynamically into an Image component.

               

              Jason

              • 4. Re: Embed images programatically
                Francisc Level 3

                I thought that might be the case, thank you!

                 

                About my second question, I want to concatenate embedded vars like this:

                 

                var name:String="image";

                myImage.source=this["_"+name];

                 

                _image is an Embded image.

                 

                The code above throws an error. How can I do this please?

                • 5. Re: Embed images programatically
                  Jason Szeto Level 3

                  What error are you seeing?

                   

                  This works for me:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                         xmlns:s="library://ns.adobe.com/flex/spark"
                                         xmlns:mx="library://ns.adobe.com/flex/mx">
                      <fx:Declarations>
                          <!-- Place non-visual elements (e.g., services, value objects) here -->
                      </fx:Declarations>
                     
                      <fx:Script>
                          <![CDATA[
                              [Embed("myImage.jpg")]
                              public var afImage:Class;
                             
                              public function loadImage():void
                              {
                                  var c:Class = this["afImage"];
                                  img.source = c;
                              }
                             
                          ]]>
                      </fx:Script>
                     
                      <s:layout>
                          <s:VerticalLayout/>
                      </s:layout>
                     
                      <s:Button label="Load" click="loadImage()"/>
                      <mx:Image id="img"/>
                     
                  </s:Application>

                  • 6. Re: Embed images programatically
                    Francisc Level 3

                    Well, for starters I am using Flex 3, not 4.

                    The error is at compile time and the first line is:

                    ReferenceError: Error #1069: Property _close not found on MyClass and there is no default value.

                     

                    I test this by setting it as icon for an Alert window.

                    If I put _close it works.

                    If I put this["_close"], Flex Builder doesn't throw an error but the compiler does, the one above.

                     

                    Thank you.