Search code examples
actionscript-3bitmapdata

AS3 - Is there an equivalent decode() to BitmapData.encode()?


Is there a way to turn the ByteArray back into a BitmapData after using BitmapData.encode()?


Solution

  • No, there isn't. You have to use Loader to load encoded JPEG and access bitmap data through the loaded content:

        var bitmap:Bitmap = new Bitmap(new BitmapData(100, 100, false, 0xFF0000));
        addChild(bitmap);
    
        var bytes:ByteArray = new ByteArray();
        bitmap.bitmapData.encode(bitmap.bitmapData.rect, new JPEGEncoderOptions(), bytes);
        bitmap.bitmapData.dispose();
    
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void
        {
            var bd:BitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
    
            bitmap.bitmapData = bd;
        });
        loader.loadBytes(bytes);