9 Replies Latest reply on Jul 4, 2009 5:04 PM by Ansury

    Loading an Image from a local File (AIR/Flex)

    Ansury Level 3

      var f:File = (assume it's a valid image file reference)

       

      var i:Image = new Image();

      i.source = f.nativePath;

       

       

      Kinda makes sense right? Except it doesn't display properly when the image is added unless I also specify a width and height on the new Image. This may not be a problem in many applications that hard code the size anyway, but I want to use the actual size of the image, without rescaling.

       

      i.width = whatever;

      i.height = whatever;//But I don't want to hard code these values, I want "actual" image size!

       

      Anyone know of a way to get around this problem? Am I missing a step here? Web resources on this specific topic are very lacking.

        • 1. Re: Loading an Image from a local File (AIR/Flex)
          sri harsha namala Level 2

          Hi,

                    This is not possible in Flex ,But its possible in AIR. Use Image load Property after selecting file ,Once check the following code.

           

                    <?xml version="1.0" encoding="utf-8"?>
          <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" creationComplete="init()">
              <mx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
                      private var f:File;
                      [Bindable]
                      private var imgPath=null;
                      private function init():void
                      {
                          f=new File();
                          f.addEventListener(Event.SELECT,SelectFile);
                      }
                      private function SelectFile(e:Event)
                      {
                          Alert.show("Selected Path "+f.nativePath);
                          img.load(f.nativePath);   
                      }
                  ]]>
              </mx:Script>
              <mx:Button id="bn" click="{f.browse()}" label="Load" />
              <mx:Image id="img"  />
          </mx:WindowedApplication>

          • 2. Re: Loading an Image from a local File (AIR/Flex)
            Ansury Level 3

            But what if you want to create an array of Images outside of MXML?  Using load() you still need to set the width and height of the Image if it was created via the new keyword, otherwise it's not visible when you try to display it.  If you have an application that is processing a set of images (which may or may not ever be displayed) there seems to be no way of loading these files inside a script block.  Maybe I need to rethink my approach here.. but it shouldn't behave like this.

             

            I think this is incorrect (buggy) behavior.  Why can't the actual width/height of an Image be set once it's loaded?

            • 3. Re: Loading an Image from a local File (AIR/Flex)
              Michael Borbor Level 4

              If you use a loader, using the loaderInfo will give you access to the width

              and height of the image.

              • 4. Re: Loading an Image from a local File (AIR/Flex)
                Ansury Level 3

                I guess my thoughts are: we should be able to be lazy(er)!

                 

                Why should we have to create an event listener and all that when it should have been two lines of code?  Or for that matter, why can't we just type new Image(fileReference) or new Image(filePath) ?

                • 5. Re: Loading an Image from a local File (AIR/Flex)
                  Michael Borbor Level 4

                  I haven't worked that much with AIR, but sure it's worth finding 

                  what's going on, I'm kinda sure it's related to the component livecycle.

                   

                  Sincerely,

                   

                  Michael

                   

                  Sent from my iPhone

                   

                  El 03/07/2009, a las 23:59, Ansury <forums@adobe.com> escribió:

                   

                  >

                  I guess my thoughts are: we should be able to be lazy(er)!

                  >

                  Why should we have to create an event listener and all that when it 

                  should have been two lines of code?  Or for that matter, why can't 

                  we just type new Image(fileReference) or new Image(filePath) ?

                  >

                  • 6. Re: Loading an Image from a local File (AIR/Flex)
                    Michael Borbor Level 4

                    Even though an Image control inherits from a SWFLoader, I wish I'd 

                    have installed the SDK in order to do some testing.

                    • 7. Re: Loading an Image from a local File (AIR/Flex)
                      Ansury Level 3

                      I was considering checking out the source, but given the fact that my whole point is to be lazy here, it seemed a little counter productive.  Usually when I start digging into the Flex source it turns into a big time waster! 

                      • 8. Re: Loading an Image from a local File (AIR/Flex)
                        Ansury Level 3

                        I would think that this crummy custom Image class hack would work, but surprise surprise -- the width/height in loaderInfo is flat out WRONG when the event fires. It seems to have a hard coded width/height no matter what the file size. Silly!

                         

                         

                        <mx:Image xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
                             
                             <mx:Script>
                                  <![CDATA[
                                       public function init():void {
                                            
                                            this.addEventListener(Event.COMPLETE, loaded);
                                       }
                                       
                                       private function loaded(e:Event):void {
                                            
                                            this.width = this.loaderInfo.width;
                                            this.height = this.loaderInfo.height;
                                       }
                                  ]]>
                             </mx:Script>
                             
                        </mx:Image>
                        
                         
                        

                         

                         

                        Turns out, this version DOES work (but it's still a hack fix in my opinion):

                         

                         

                        <mx:Image xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
                             
                             <mx:Script>
                                  <![CDATA[
                                       public function init():void {
                                            
                                            this.addEventListener(Event.COMPLETE, loaded);
                                       }
                                       
                                       private function loaded(e:Event):void {
                                            
                                            this.width = this.contentWidth;
                                            this.height = this.contentHeight;
                                       }
                                  ]]>
                             </mx:Script>
                             
                        </mx:Image>
                        
                         
                        

                         

                         

                        You guys at Adobe are so silly.    It would seem to me that Image's width and height should be set (rather than left at 0) once the content's width and height is known.  So now that I've fixed this bug (as far as I'm concerned) for ya Adobe, I take cash, cashier's check, or money orders--thanks!

                         

                        But really, if this doesn't work this way in Flex 4, I think it should.

                        • 9. Re: Loading an Image from a local File (AIR/Flex)
                          Ansury Level 3

                          Trying to set question as "answered" since it didn't work by editing the above post... god these forums suck.