在Flex中同步从外部资产图像中获取ByteArray

时间:2011-10-22 00:18:58

标签: flex bytearray flex4.5

我有一个var,它保存图像路径的String值。 如何使用它来同步从该图像中获取ByteArray?

谢谢。

1 个答案:

答案 0 :(得分:2)

您无法同步执行此操作。但这是你如何异步地做到这一点。

<?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" />