After a week of trying different methods, my last resort is to ask you guys about my problem with ActionScript Loader.

I cannot load an image from the URL using Loader as it works on my another application. I simply don't know where is the difference in the code. I cannot even debug the content of Loader object because simply trying to peek what's in there throws an exception.

My code is:

urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, onLoadedBytes);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, passEvent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, passEvent);
urlLoader.load(new URLRequest(url));

Normal loader:

loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, passEvent);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,   passEvent);
loader.load(new URLRequest(url), new LoaderContext(true));

Loading related methods:

private function onLoadedBytes(e:Event):void
{
    var ba:ByteArray = e.target.data;
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
    loader.loadBytes(ba);
}
private function onImageLoaded(e:Event):void
{
    _bitmapData = e.target.content.bitmapData;
    dispatchEvent(new Event(Event.COMPLETE));
}

Any idea how to fix it? The domain I try to load an image from is VK Games (http://vk.com/playfreegames) and the image is app-game related.

I've searched the stack for an answer, but none of the solutions presented solved my case.

有帮助吗?

解决方案

It's a security issue. From the docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html

"If the loaded content is an image, its data cannot be accessed by a SWF file outside of the security sandbox, unless the domain of that SWF file was included in a URL policy file at the origin domain of the image."

If you listen to the security events, you'll probably see it giving an error. Loader works because you're just accessing the image, so it's fine, however URLLoader in BINARY mode won't as you're trying to access the pixel data, hence the security error. If you tried to access the pixel data through the Loader, you'll probably get the same error.

Either just load it without accessing the BitmapData behind (includes applying filters etc), or use a cross domain file

其他提示

If I remember right, having SWF file on server A and images on server B requires a crossdomain.xml file on server B, in order to allow the swf to load the images.

http://kb2.adobe.com/cps/142/tn_14213.html

-- I would recommend to use a library for loading like LoaderMax or Hydotics/Queloader...

greensock com/loadermax/

github com/hydrotik/QueueLoader/wiki

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top