4 Replies Latest reply on Apr 22, 2010 11:01 AM by avdave

    Problem casting a string to image

    avdave

      Hello,

        I am  rather new to flash builder and currently having problem to cast a string to an image in my function. Here's the details

       

      1) I have declared few image holders as below:

                <mx:Image id="img0" x="40" y="80" width="250" height="550" mouseDown="img0_mouseDownHandler(event)"/>

      <mx:Image id="img1" x="298" y="80" width="240" height="270" mouseDown="img1_mouseDownHandler(event)"/>

       

       

      2) Through Http Service I am then getting the rss feed of xml in my result handler function.

          Also, in the same function I am building xmlList for my photo locations in the XML

              photoXMLList = tXML.entry.link.(@rel == 'enclosure').@href;

       

      3) After this I am looping through the index to traverse xm llist and place the photo into image place holders based on the current index of traversal:

       

      //declaration

      var strIndx:String; // string holds dynamically generated image id

      var index: int;

      var image:Image;

       

      //initialize index

      image = new Image();

      index=0;

      img= "img";

       

      //loop through index and ge

       

      for (n=index; n <=index+4;n++)

      {

                strIndx = img +(index.toString());// make the strIndx to image id of current value e.g. first value would be img0 here

          

                if (tXML.entry[index].category.@term == "550")

                  {

                      if ( index == 0 )

       

                          {

                            

                                    Alert.show(strIndx); // shows img0

       

                                // problem here...not able to cast the indx containing string value of image id i.e. img0

                                //    into image component,  more importantly image id

                                  image=mx.controls.Image(strIndx);

       

                                  image.source = photoXMLList[index];

                          }

                }

       

       

       

       

      }

       

       

       

      As explained in above code I am not able cast the string value of image id's (strIndex in above case) to actual Image value. I have tried to do that using casting to image variable but flex is not conevrting it for some reasons.

       

      I would  Highly appreciate prompt help on this!

       

      Thanks

      Akshay

        • 1. Re: Problem casting a string to image
          David_F57 Level 5

          hi,

           

          i'm having a little trouble trying to work out what you are trying to do, but if I have an'array' of images then

           

          for (var i:Number=0; i < data.length; i++)

          {

               var img:Image= new Image();

               img.id = "img"+String(i);

               img.addEventListener(MouseEvent.CLICK,myImageClicked);

               img.source=data[i].imagename;

          }

           

          now I have 10 images that have ids from img0 to img9, an event that is called if they are clicked each image is loaded from a collection called data which contains the name of the image.

           

          David.

          • 2. Re: Problem casting a string to image
            avdave Level 1

            Thanks David,

               That sounds exactly what I wanted. Sorry for posting this question on multiple forum. I am using flash builder now so thought of posting it on this forum too.

             

               Based on you reply I amended my previous code as below:

             

                  image.id = img +String(index);

             

               and then load the source

                  image.source = photoXMLList[index];

             

             

               but still  no luck . but if I would put the id explicitly as below

             

                img0.source = photoXMLList[index]; // id of first max: Image

             

               it just works fine ......not sure what is wrong here

             

             

            Thanks

            Akshay

            • 3. Re: Problem casting a string to image
              David_F57 Level 5

              hi,

               

              When loading images it's not quite as simple as sample I gave, I was just trying to get a feel for what you where doing,

               

              I have an example of loading multiple images by their bytedata into an array then use that for display, It is based on local files but the principle is the similar. Rather than using the filereference loader you would use a url loader.

               

              http://gumbo.flashhub.net/pagedrop/

               

              David

              • 4. Re: Problem casting a string to image
                avdave Level 1

                Thanks David for your patient and helpful replies so far..

                 

                   Here's the full code.. Please let me know what you think

                 

                Thanks

                Akshay

                 

                ------------------------------------------------------ Code--------------------------------------------------------------------------- --------

                 

                 

                <?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" minWidth="1250" creationComplete="photoService_T.send(),photoService_O.send()"  minHeight="600" backgroundColor="black">

                <fx:Style source="Flickr_Test.css"/>

                <fx:Script>

                <![CDATA[

                import mx.collections.ArrayCollection;

                import mx.controls.Alert;

                import mx.controls.Image;

                import mx.effects.Resize;

                import mx.events.FlexEvent;

                import mx.events.ItemClickEvent;

                import mx.events.ListEvent;

                import mx.events.MoveEvent;

                import mx.events.ResizeEvent;

                import mx.managers.PopUpManager;

                import mx.rpc.events.FaultEvent;

                import mx.rpc.events.ResultEvent;

                import mx.utils.object_proxy;

                 

                 

                 

                [Bindable]

                public var photoXMLList:XMLList;

                private var img:Image;

                 

                private var image:Image;

                public var photoXMLList_new:XMLList;

                //private var img_new:Image;

                 

                var nsDefault:Namespace = new Namespace("http://www.w3.org/2005/Atom");

                 

                 

                private function photoResultHandler_T(re:ResultEvent):void {

                         image = new Image();

                 

                          default xml namespace = nsDefault;

                         var img:String;

                        var indx: String;

                 

                         var booltest:Boolean;

                 

                          img="img";

                 

                          var tXML:XML = re.result as XML;

                     photoXMLList = tXML.entry.link.(@rel == 'enclosure').@href;

                 

                    var n:int;

                     for (n=0; n <=5;n++)

                        {

                                image.id = img +String(n);

                              Alert.show(image.id.toString());

                    

                            if (tXML.entry[n].category.@term == "550")

                               {

                                       if ( n == 0 )

                 

                                        {

                                         Alert.show(image.id.toString());

                 

                                         image.source = photoXMLList[n];

                                         }

                                }

                 

                 

                 

                 

                 

                 

                     }

                 

                 

                }

                 

                 

                private function photoFaultHandler_T(fe:FaultEvent):void {

                Alert.show(fe.fault.message);

                }

                 

                protected function photoResultHandler_O(rew:ResultEvent):void

                {

                // TODO Auto-generated method stub

                default xml namespace = nsDefault;

                var tXML_new:XML = rew.result as XML;

                photoXMLList_new = tXML_new.entry.link.(@rel == 'enclosure').@href;

                }

                 

                 

                protected function photoFaultHandler_O(fen:FaultEvent):void

                {

                // TODO Auto-generated method stub

                Alert.show(fen.fault.message);

                }

                 

                 

                 

                ]]>

                </fx:Script>     

                 

                <fx:Declarations>

                <s:HTTPService id="photoService_T" url="http://api.flickr.com/services/feeds/photoset.gne?set=72157623742603753&amp;nsid =48951865@N07" resultFormat="e4x" result="photoResultHandler_T(event);" fault="photoFaultHandler_T(event);"/>

                <s:HTTPService id="photoService_O" url="http://api.flickr.com/services/feeds/photoset.gne?set=72157623867029406&amp;nsid =48951865@N07" resultFormat="e4x" result="photoResultHandler_O(event);" fault="photoFaultHandler_O(event);"/>

                <mx:WipeDown id="image_addedEffect" startDelay="50"></mx:WipeDown>

                <mx:Parallel id="image_removedEffect">

                <mx:Zoom />

                <mx:Fade />

                </mx:Parallel>

                </fx:Declarations>

                 

                 

                <mx:Image id="img0" x="40" y="80" width="250" height="550" />

                 

                <mx:Image id="img1" x="974" y="80" width="250" height="550" mouseDown="img6_mouseDownHandler(event)"/>

                </s:Application>