1 Reply Latest reply on Nov 29, 2011 11:05 AM by Flex harUI

    How do I save/load a Image "object"?

    devInterama

      Hi,

       

      I have a spark Image and i want to save/load it from a file. But im not trying to save as png or jpg, but the whole image object. I'm doing as follow but when I load it, it is null.

       

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
                          xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
      
                <fx:Script>
                          <![CDATA[
                                    import spark.components.Image;
      
      
                                    private var img:Image = new Image;
      
                                    protected function save(event:MouseEvent):void
                                    {
                                              var fl:File = File.documentsDirectory.resolvePath('savetest/test.tst' );
                                              img = new Image;
                                              img.addEventListener(Event.COMPLETE, function(){
                                                        try
                                                        {
                                                                  var fs:FileStream = new FileStream;
                                                                  fs.open(fl, FileMode.WRITE);
                                                                  fs.writeObject(img);
                                                                  fs.close();
      
                                                                  trace('# save ok');
                                                        }
                                                        catch(error:Error) 
                                                        {
                                                                  trace('# save error', error);
                                                        }
                                              });
                                              img.source = '../img.png';
      
                                              addElement(img);     // add/remove just to the img be loaded
                                              removeElement(img);
                                    }
      
                                    protected function load(event:MouseEvent):void
                                    {
                                              var img:Image;
                                              var fl:File = File.documentsDirectory.resolvePath('savetest/test.tst');
      
                                              try
                                              {
                                                        var fs:FileStream = new FileStream;
                                                        fs.open(fl, FileMode.READ);
                                                        img = fs.readObject() as Image;
                                                        fs.close();
      
                                                        trace('# load ok', img);
                                              } 
                                              catch(error:Error) 
                                              {
                                                        trace('# load error', error);
                                              }
      
                                              addElement(img);     // here it is null 
                                    }
      
                          ]]>
                </fx:Script>
      
                <fx:Declarations>
                          <!-- Place non-visual elements (e.g., services, value objects) here -->
                </fx:Declarations>
                <s:Button x="173" y="113" label="Save" click="save(event)"/>
                <s:Button x="347" y="113" label="Load" click="load(event)"/>
      </s:View>
      

       

      Thank you.