ActionScript: is image possible in base64?

Is it possible to convert the selected image to a base64 encoded string?

It would be a nice and easy solution for the image downloader. :)

Thanks;)

+6
source share
2 answers

If you want to encode a byte array of the loaded image, you can use the Base64Encoder class from mx.utils Base64Encoder .

Sort of:

var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); loader.load(new URLRequest("img.jpg")); function loadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete); var bmd:BitmapData = Bitmap(e.target.content).bitmapData; var ba:ByteArray = bmd.getPixels(new Rectangle(0,0,bmd.width,bmd.height)); var b64:Base64Encoder = new Base64Encoder(); b64.encodeBytes(ba); trace(b64.toString()); } 

I had to track the class here .

Also, there is another Base64 class that I found but not tested here ... but it seems to work the same way.

Hope this helps.

+7
source

You can save the image as a Base64 string, but I would not recommend it. I tried to do this and it greatly slows down your application.

If you still want to do this, you should download the Base64 class from this link: http://garry-lachman.com/2010/04/21/base64-encoding-class-in-actionscript-3/

If you then get bitmapData from your image, you can call the .getPixels () method, which returns bytearray. This bytearray can be converted to Base64-string using the class in the link.

If you want to load images from a Base64 string, you can create a Loader object and use loadBytes (), the load method in byteArray, which you get by decoding your Base64 string.

Hope this helps :)

+2
source

Source: https://habr.com/ru/post/892130/


All Articles