AS3/AIR/Mobile - 将位图保存到文件

ato*_*oid 5 image bitmap actionscript-3 bitmapdata

我认真地到处寻找,但找不到解决方案.我的应用程序可以轻松地将UTF字节作为XML文件保存在iOS和Android的允许缓存位置中.

但我无法弄清楚如何将Bitmap/BitmapData保存到同一缓存文件夹中的文件.创建文件很好....

_fs.open(_f, FileMode.WRITE);

//What goes in here to save the Bitmap?

_fs.close();
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的帮助将不胜感激.

谢谢

更新:代码如下......

        _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();
Run Code Online (Sandbox Code Playgroud)

更新 - 答案

//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();
Run Code Online (Sandbox Code Playgroud)
//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);
}
Run Code Online (Sandbox Code Playgroud)

jau*_*oux 1

如果您正在写入设备缓存并且更关心速度而不是文件大小,您可以考虑使用:

bd.encode(bd.rect, new PNGEncoderOptions(true), ba);
Run Code Online (Sandbox Code Playgroud)

请注意,如果没有 fastCompression = true,png 编码器比 jpeg 编码器慢

有关 as3 png 编码性能的更多信息请参见此处

如果您首先需要速度(对于编码和解码),您可以以原始格式编写位图数据,如果您已经知道大小,则可以不带标头;如果您想嵌入图像大小并允许更轻松地查看,则可以使用 bmp 等基本标头你的缓存

顺便说一句,我不确定为什么您需要首先重新编码位图:似乎位图已经来自加载器,所以_loader.contentLoaderInfo.bytes应该已经为您提供了刚刚加载的文件的 ByteArray,它应该已经编码,并且将避免对可能已经压缩的文件进行有损压缩。