Dan's Tech Den For the Cyber Warrior

3Nov/120

Embed Images as binaryData in AS3 to reduce SWF size

This evolved from a lengthy discussion on the file size of an SWF.

It seems that Flash/Flex embeds PNGs as 32bit only. And this in some cases causes the size of the image to be greatly increased. In some cases the size of the image more than doubles if the image had been compressed using optimization utilities.

The solution to keep the reduced size that I came up with is to embed the image as binaryData instead of a bitmap using the mimeType='application/octet-stream' like this

[Embed(source = "assets/8bit.png", mimeType = "application/octet-stream")]
public static const IMG:Class;

But this gives us a ByteArray instead of the needed bitmap.

This ByteArray can be decoded into a bitmap using the Loader.loadBytes() function, but this function is asynchronous and thus this step needs to be performed in advance, during initial game loading.

For this I have made the following OctetStreamBitmapDecoder class