Francisc
Francisc

Reputation: 80505

Get ByteArray from external asset image synchronously in Flex

I have a var that holds the String value for the path to an image. How can I use that to get the ByteArray from that image synchronously?

Thank you.

Upvotes: 0

Views: 969

Answers (1)

Greg
Greg

Reputation: 600

You can't do it synchronously. But here is how you do it asynchronously.

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[
        public function init():void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
            loader.load(new URLRequest(encodeURI("http://www.google.com/logos/2011/mary_blair-2011-hp.jpg"))); 
        }

        private function onComplete(e:Event)
        {
            //of course if all you were doing is displaying an image its better to do:
            //image.source = e.currentTarget.content;

            var bytes:ByteArray = LoaderInfo(e.currentTarget).bytes;

            //binary output
            trace(bytes);

            var imageLoader:Loader = new Loader();
            imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageComplete);
            imageLoader.loadBytes(bytes, null);
        }

        private function onImageComplete(e:Event):void
        {
            image.source = LoaderInfo(e.currentTarget).content;
        }
    ]]>
</fx:Script>

<s:Image id="image" />

Upvotes: 2

Related Questions