Turn your BitmapData into a string and back! Great for storing images in XML, Databases, or transmitting them over a network. This post will cover Bitmap Encoding, PNG Encoding and JPG Encoding along with the pros and cons for each.
Bitmap Encoding – (link)
// Encode var encoded:String = BitmapEncoder.encodeBase64(bitmap.bitmapData); // Decode var bd:BitmapData = BitmapEncoder.decodeBase64(encoded);
Advantages:
Keep transparency
Fastest Method
Does not require a Loader
Disadvantages:
Largest Filesize
Will only work with images up to a certain size
PNG Encoding – (link)
// Encode var ba:ByteArray = PNGEncoder.encode(bitmap.bitmapData); var encoded:String = Base64.encodeByteArray(ba); // Decode var decoded:ByteArray = Base64.decodeToByteArray(encoded); var loader:Loader = new Loader(); loader.loadBytes(decoded);
Advantages:
Keep transparency
Smaller filesize than Bitmap Encoding
Disadvantages:
Slower than Bitmap Encoding
Larger than JPG Encoding
Requires Loader
JPG Encoding – (link)
// Encode var jpgEncoder:JPGEncoder = new JPGEncoder(); var ba:ByteArray = jpgEncoder.encode(bitmap.bitmapData); var encoded:String = Base64.encodeByteArray(ba); // Decode var decoded:ByteArray = Base64.decodeToByteArray(encoded); var loader:Loader = new Loader(); loader.loadBytes(decoded);
Advantages:
Smallest Filesize
Variable Quality
Disadvantages:
Slower than Bitmap Encoding
No Transparency
Requires Loader
For the bitmap converted into a String, what about using the core zip library and re-encode it in base 64?
It might be slower but the size should be smaller.
Something to consider?
The string is the result of Base64 encoding the ByteArray. I’m not sure what the effect would be of Base64 encoding a Base64 encoded string
Is there any as2 equivalent?
Thanks gonna try this now.