views:

2754

answers:

1

I'm doing a simple test. I want to write BitmapData into a ByteArray. I'm trying to do this with writeObject() and readObject(). Read object seems to have trouble making sense of the BitmapData.

var byteArray : ByteArray = new ByteArray();
var _cache : BitmapData = new BitmapData( 640, 480, true, 0x000000 );
var _blank : BitmapData = new BitmapData( 640, 480, true, 0x000000 );

byteArray.writeObject( _blank );
byteArray.position = 0;
_cache = byteArray.readObject() as BitmapData;
trace( _cache ); // Traces null

Can anyone clear this up for me? I can't make sense of what's going wrong with readObject();

I know I can do this getPixels() and setPixels(), but I'd like to treat the object as an object here.

+3  A: 

you should use BitmapData::getPixels and BitmapData::setPixels ... writing class instances to a ByteArray as such never completely works as expected ... try with Sprites ... :) ... this is mostly due to the fact, that the default implementation somehow only writes an object's property to the IDataOutput (ByteArray or Socket) ... the pixels are not a property of the BitmapData in that sense ... the BitmapData is just an ActionScript wrapper, that exposes an interface to some internal flash player data structure ...

you should write an adapter for BitmapData, that implements IExternalizable, so that you have control, over what happens ... essentially, you will only need to read/write the dimensions, and the actual pixel payload ... maybe the transparent-flag ... remember to use registerClassAlias ...

back2dos
Really interesting. Awesome answer, as usual. Thank you! I'll experiment with this.
grey