AS3 / AIR / Mobile - save raster image to file

I seriously looked everywhere, but I can not find a solution. My application easily saves UTF bytes as XML files in the allowed cache cells for both iOS and Android.

But I can not decide 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.

thank

UPDATE: The 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);
}
+5
source share
1 answer

If you write to the device’s cache and are more interested in speed than file size, you can use:

bd.encode(bd.rect, new PNGEncoderOptions(true), ba);

note that without fastCompression = true, the png encoder is slower than the jpeg encoder

as3 png

( ), bitmapdatas , , , , bmp,

, , : , , _loader.contentLoaderInfo.bytes ByteArray , , , , .

+1

All Articles