3

How to save an Image into SharedObject? I'm using SharedObject for save application state,It is working well for Text Object and UIComponent well,but saving an Image create problem.It doesn't throw any error.

   userdiagram.imageData = zorder.getItemAt(i) as mx.controls.Image
   userdiagram.x        = (zorder.getItemAt(i)).x;
   userdiagram.y        = (zorder.getItemAt(i) ).y;
   userdiagram.height   = (zorder.getItemAt(i)).height
   userdiagram.width    = (zorder.getItemAt(i)).width

here userdiagram is class and imageData is a image type variable.

Devendra
  • 1,864
  • 6
  • 29
  • 49

2 Answers2

3

You have to use byteArray to save the image in the sharedObject.

You can read more about it here : http://www.kirupa.com/forum/showthread.php?306972-Saving-bitmaps-to-a-shared-object

abnvp
  • 1,037
  • 11
  • 19
  • what is the size of the image that you are saving? – abnvp Feb 18 '13 at 08:46
  • From adobe documentation about SharedObject: "The default size limit is 100 KB, although it can be larger if the client allows it." – abnvp Feb 18 '13 at 09:56
1

I save the Image as suggest link by Abhinav:

var data:BitmapData = new BitmapData((zorder.getItemAt(i) as mx.controls.Image).width, (zorder.getItemAt(i) as mx.controls.Image).height);
                data.draw((zorder.getItemAt(i) as mx.controls.Image));
                var encoder:JPGEncoder = new JPGEncoder();
                var bytes:ByteArray = encoder.encode(data);

                userdiagram.x         = (zorder.getItemAt(i)).x;
                userdiagram.y         = (zorder.getItemAt(i) ).y;
                userdiagram.height    = (zorder.getItemAt(i)).height
                userdiagram.width     = (zorder.getItemAt(i)).width
                userdiagram.rotation    = (zorder.getItemAt(i)).rotation
                userdiagram.bytes = bytes;
                saveState.addItem(userdiagram)

But when I retrieve image from sharedObject create a image type object and use the following code:

var v:Object = _saveIn.data.value1.readObject();
p.source   = (v as ArrayList).getItemAt(i).bytes;

here p is Image class object and (v as ArrayList).getItemAt(i) is saved byte array location.

Devendra
  • 1,864
  • 6
  • 29
  • 49