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

时间:2012-05-17 14:53:30

标签: image actionscript-3 bitmap bitmapdata

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

但我无法解决如何将Bitmap / BitmapData保存到同一缓存文件夹中的文件的问题。 创建文件很好....

_fs.open(_f, FileMode.WRITE);

//What goes in here to save the Bitmap?

_fs.close();

对此的任何帮助将不胜感激。

由于

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

        _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();

更新 - 答案

//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);
}

1 个答案:

答案 0 :(得分:1)

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

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

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

有关as3 png编码效果的更多信息here

如果您首先需要速度(对于编码和解码),您可以用原始格式写入bitmapdatas,如果您已经知道大小则没有标题,或者如果您想要嵌入图像大小,则使用像bmp这样的基本标题允许更轻松地查看缓存

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

相关问题