
By Jesse Warden:
Flash Player 10 comes with a nifty new method for theflash.net.FileReference class called “save“. What’s dope is that you can send it a ByteArray, and it’ll write it verbatim. This means, saving images out of your Flash & Flex Applications is wicked fast now AND gets around that lamesauce security restriction that plagued a lot of older Flash Player 9 code. In the past, you had to either upload the image, ask the server to convert/save a ByteArray/Base64 encoded string for you somewhere, which you would then download via FileReference.download. That code would fail in Flash Player 10 because you cannot initiate a download unless it’s via a mouse click.
The problem I’ve found is getting the image directly, meaning, without converting it or re-compressing it before saving. Here’s the 4 ways you can get an image into a ByteArray for saving:
- Use a Loader, and upon Event.COMPLETE, cast the the Loader.content as a Bitmap, and throw to a JPEGEncoder/PNGEncoder.
- Use a URLStream, and upon Event.COMPLETE, copy the bytes out into a new ByteArray.
- In Flex, use an mx:Image and repeat #1.
- In Flex, use Ely’s SuperImage, and repeat #1… or modify his class to expose the BitmapData it caches.
Read more at: http://jessewarden.com/2009/01/saving-images-out-of-flashflex-without-losing-quality.html
