6 Replies Latest reply on Dec 14, 2009 6:39 AM by IlyaG

    How can I store bitmap data in SharedObject class?

    IlyaG Level 1

      Any ideas?

       

      That what I been trying to do. First run the "First code" then the "Second code".

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
           <![CDATA[
                import mx.controls.Image;
                private function init():void
                {
                     var o:SharedObject = SharedObject.getLocal("t1","/");
                     
                     //First code
                     o.clear();
                     var bit:Bitmap= new Bitmap(new BitmapData(100,100,false,0));
                     o.data.bit=bit;
                     o.flush();
                     
                     //Second code               
                     var bit:Bitmap = o.data.bit;
                     var pic:Image = new Image();
                     pic.source=bit;
                     Application.application.addChild(pic);
                     
                     
                }
           ]]>
      </mx:Script>     
      </mx:Application>
      
        • 1. Re: How can I store bitmap data in SharedObject class?
          PhilipKeiter Level 2

          Hello godilya,

           

          There are several problems with your code. It should look like this:

           

          <?xml version="1.0" encoding="utf-8"?>

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();">

          <mx:Script>

              <![CDATA[

           

                  import mx.core.Application;

                  import mx.controls.Image;

           

                  private function init():void

                  {

                      var o:SharedObject = SharedObject.getLocal("t1","/");

           

                      //First code

                      o.clear();

                      var bit:Bitmap= new Bitmap(new BitmapData(100,100,false,0));

                      o.data.bit=bit;

                      o.flush();

           

                      //Second code

                      var bit2:Bitmap = o.data.bit;

                      var pic:Image = new Image();

                      pic.source=bit2;

                      Application.application.addChild(pic);

                  }

           

              ]]>

          </mx:Script>

          </mx:Application>

           

          I ran this to confirm it works (assuming you are using Flex 3).

           

          Thanks,

          Philip

          • 2. Re: How can I store bitmap data in SharedObject class?
            IlyaG Level 1

            But what did you changed???

             

            It is working because you didn't deleted the first part of the code and you initializing the SharedObject inside the code. To test if this is really working you have to run it once when you deleted all the "Second code" and the when you deleted all the "First code".

            • 3. Re: How can I store bitmap data in SharedObject class?
              PhilipKeiter Level 2

              godilya,

               

              Sorry about that. Since there were a bunch of errors in your code I just fixed them all. But you are right that even after it still does not actually save it to the SO.

               

              I would recommend saving it as a ByteArray, as it is specifically listed in the documentation as one of the classes supported by the SharedObject.

               

                          //First code

                          var bitmap:Bitmap= new Bitmap(new BitmapData(100,100,false,0xFF0000));

                          var bitmapData:BitmapData = bitmap.bitmapData;

                          var byteArray:ByteArray = bitmapData.getPixels(bitmapData.rect);

                          so.clear();

                          so.data.byteArray = byteArray;

                          so.data.width = bitmapData.rect.width;

                          so.data.height = bitmapData.rect.height;

                          so.flush();

               

                          //Second code

                          var byteArray:ByteArray = ByteArray(so.data.byteArray);

                          var bitmapData:BitmapData = new BitmapData(so.data.width, so.data.height);

                          bitmapData.setPixels(bitmapData.rect, byteArray);

                          var bitmap:Bitmap = new Bitmap(bitmapData);

                          var pic:Image = new Image();

                          pic.source=bitmap;

                          Application.application.addChild(pic);

               

               

              I tested this one properly, with one commented out then the other.

               

              Thanks,

              Philip

               

              Message was edited by: Philip Keiter

              • 4. Re: How can I store bitmap data in SharedObject class?
                IlyaG Level 1

                I don't know then... I just copy past your code and it's not working. When I run the second code it shows nothing on my screen.

                To ensure that it is working for you, try making two separate programs and load the shared object. It is very long time that I am trying to figure this out.

                 

                I hope you be able to help me.

                • 5. Re: How can I store bitmap data in SharedObject class?
                  PhilipKeiter Level 2

                  Ah, if you are using two separate programs there may be an issue with them being able to access each others SharedObjects. Are they both in the same domain?

                  • 6. Re: How can I store bitmap data in SharedObject class?
                    IlyaG Level 1

                    I just test it again and I see that it does working!

                     

                    This is the final source with a normal sample:

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                     creationComplete="init()">
                    <mx:Script>
                         <![CDATA[
                              
                              import mx.controls.Image;
                              import mx.core.Application;
                              
                              private var bitmap:Bitmap;
                              
                              private function init():void
                              {
                                   bitmap = new Bitmap(new BitmapData(100,100,false,0xFF0000));
                                img.source=bitmap;
                            }
                              
                              private function clearImage():void
                              {
                                   img.source = new Bitmap();
                              }
                              
                              private function saveImage():void
                              {
                                   var so:SharedObject = SharedObject.getLocal("t1","/");
                                   so.clear();
                                   if (bitmap.bitmapData)//test if the bitmapData is not null
                                        var byteArray:ByteArray = bitmap.bitmapData.getPixels(bitmap.bitmapData.rect);
                                else return;
                                so.data.byteArray = byteArray;
                                    so.data.width = bitmap.bitmapData.rect.width;
                                    so.data.height = bitmap.bitmapData.rect.height;
                                so.flush();
                              }
                              
                              private function loadImage():void
                              {
                                   var so:SharedObject = SharedObject.getLocal("t1","/");
                                   if (so.data.byteArray==null)
                                        return;
                                   var byteArray:ByteArray = so.data.byteArray;
                                var bitmapData:BitmapData = new BitmapData(so.data.width, so.data.height);
                                bitmapData.setPixels(bitmapData.rect, byteArray);
                                img.source = new Bitmap(bitmapData);
                                   
                              }
                         ]]>
                    </mx:Script>     
                         <mx:Button x="10" y="10" label="Clear Image" id="clear" click="clearImage()"/>
                         <mx:Image width="100" height="100" horizontalCenter="0" top="10" id="img"/>
                         <mx:Button x="10" y="88" label="Load image" cornerRadius="10" click="loadImage()"/>
                         <mx:Button x="10" y="58" label="Save image" cornerRadius="10" click="saveImage()"/>
                    </mx:Application>
                    

                     

                     

                    Tnx for your help!