I have seriously looked everywhere but can't find a solution. My app easily saves UTF bytes as XML files in the allowed cache locations for both iOS and Android.
But I can't work out how to save Bitmap / BitmapData to a file in the same cache folder. Creating the file is fine ....
_fs.open(_f, FileMode.WRITE);
//What goes in here to save the Bitmap?
_fs.close();
Any help on this would be greatly appreciated.
Thanks
UPDATE : Code is as follows...
_smallLogo = Bitmap(_loader.content);
//this.addChild(_smallLogo);
var jpg:JPGEncoder = new JPGEncoder(100);
var bd:BitmapData = new BitmapData(_smallLogo.width, _smallLogo.height);
bd.draw(_smallLogo);
var ba:ByteArray = jpg.encode(bd);
//3
_fs.open(_f, FileMode.WRITE);
_fs.writeBytes( ba, 0, ba.length );
_fs.close();
UPDATE - ANSWERED
//SAVE
_smallLogo = Bitmap(_loader.content);
var jpg:JPGEncoder = new JPGEncoder(100);
var ba:ByteArray = jpg.encode(_smallLogo.bitmapData);
_fs.open(_f, FileMode.WRITE);
_fs.writeBytes( ba, 0, ba.length );
_fs.close();
//OPEN
private function getLogo():void {
var ba:ByteArray = new ByteArray();
_fs.open(_f, FileMode.READ);
_fs.readBytes(ba);
_fs.close();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
loader.loadBytes(ba);
}
private function getBitmapData(e:Event):void {
var decodedBitmapData:BitmapData = Bitmap(e.target.content).bitmapData;
var newBMP:Bitmap = new Bitmap();
newBMP.bitmapData = decodedBitmapData;
this.addChild(newBMP);
}
If you're writing to the device cache and are more concerned with speed than file size, you might consider using:
bd.encode(bd.rect, new PNGEncoderOptions(true), ba);
please note that without fastCompression = true, png encoder is slower than jpeg encoder
more info on as3 png encoding performance here
If you need speed above all (for both encoding and decoding) you can write you bitmapdatas in raw format, with no header if you already know the size, or with a basic header like bmp if you want to embed image size and allow easier viewing of your cache
By the way, I'm not sure why you would need to reencode the bitmap in the first place:
it seems the bitmap is already coming from a Loader so _loader.contentLoaderInfo.bytes
should already get you the ByteArray of the file you just loaded, it should be already encoded, and will avoid to do lossy compression on a file wich is probably already compressed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With