6 Replies Latest reply on Jun 3, 2009 6:03 PM by Michael Borbor

    Any way to get image.width BEFORE rendering image?

    kid electric Level 1

      I am creating an Image object dynamically (in ActionScript) and adding it to a cMain (Canvas container)

       

      I want to add it at an x/y location relative to an existing object in the Canvas, so to do this I am trying to set myImage.x and myImage.y before doing a cMain.addChild(myImage)

       

      Since I would like to place the image right next to someObject in the Canvas, I wanted to use something like:

       

      myImage.x = someObject.x - myImage.width;

       

       

      The problem is that Flex does not know the width of the image.  Sure, I could specify a specific width, but I am actually doing that for the height (ie. myImage.height = someVBox.height ), so it resizes the image and draws it nicely, but won't give me the width of it.

       

      As per another discussion on this forum (http://forums.adobe.com/message/137573#137573), I attempted to determine image width in an event.COMPLETE handler, but even when loading the image is complete, height and width are still unknown (both show as "0");  it seems I could maybe determine width in event.RENDER, but wouldn't that mean the image has ALREADY been added to my Canvas and drawn on the screen?  At that point, it's a little too late, because I need to set the x coordinate of the image BEFORE rendering it.

       

      Is there any possible way to do this? (other than photo editing any images to ensure they are all certain dimensions and then hardcoding those dimensions in...)

        • 2. Re: Any way to get image.width BEFORE rendering image?
          kid electric Level 1

          okay, so I checked out that thread and it was somewhat helpful, but not entirely (maybe because I am new to Loader() and it's something I find quite confusing, but seems essential to the type of work I will be doing, so I'm trying to learn it)

           

          I was originally using a straight Image creation and then assigning Image.source (as a string URL location); I changed this to use the Loader technique as shown in that thread.

           

          So what I have is:

           

           

          var imgService:Image;
          var iImgScale:Int = 0.5; // for example... but I am actually dynamically assigning the image scale
          
          ldImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, 
          function(e:Event):void{ imgService
          .source = e.currentTarget.content;
          imgService.height = e.currentTarget.content.height * iImgScale;
          imgService.width = e.currentTarget.content.width * iImgScale;
          }); 
          
          
          


          Now, when I click the button whose event triggers the Loader, it loads the image but it tells me that the e.currentTarget.content. width and height are 0

           

          The strange thing is, if I do another load (by clicking the button again), then it pushes another image child into the Loader stack, and THIS time the height is assigned.

           

          However, I don't need a stack of children, just one (the first image)... why would it be able to grab the height on subsequent loads, but not the first one?  And if the event is "complete", how come the height/width info is still not available?

          • 3. Re: Any way to get image.width BEFORE rendering image?
            Michael Borbor Level 4

            Did you try

            event.currentTarget.width ?
            • 4. Re: Any way to get image.width BEFORE rendering image?
              kid electric Level 1

              Thanks... pretty weird how it's working, but yes, checking the height of the contentLoaderInfo instead of its actual content seemed to work to get me the correct height on the first go.

               

              I do have one more question you might be able to help with;  my image still isn't displaying because I am trying to scale it by a certain factor, and the scale is inexplicably being set to 0 (hence scaling the height/width to 0 and making the image invisible)

               

              no idea why this is happening.  I am using this:

              iImgScale = (vbContent.height - bbServices.height) / e.currentTarget.height;

               

              imgService.height = e.currentTarget.height * iImgScale;  // this takes the original image size and multiplies it by the scale factor to set the right height

               

               

              Now, for some reason, my iImgScale is coming out at 0 with these values:

              vbContent.height = 270.73

              bbServices.height = 22

              e.currentTarget.height = 399

               

              How on earth can (270 - 22) / 399 == 0???

               

              This is technically a different question than my original, so I'll mark your answer as correct, but I still need help with this (even more baffling) issue [maybe I'll post it as a new discussion since it appears to be a different issue]

              • 5. Re: Any way to get image.width BEFORE rendering image?
                kid electric Level 1

                doh!  I bet this is because i've declared iImgScale as an Int and the fraction is being rounded/floored...

                 

                EDIT: BINGO!  Just fixed it by changing type from int to Number... man I feel dumb about that one.

                 

                Thanks for the help!

                • 6. Re: Any way to get image.width BEFORE rendering image?
                  Michael Borbor Level 4

                  I'll debug your app. What type of var is iImgScale?