02.19Getting around the crossdomain.xml file when loading images in as3
Hi Guys,
I recently had to load images on from a website that doesn't contain a crossdomain.xml file. Although it works fine in the authoring environment, it's quite a pain in the butt when you put it live on a website, suddenly those images don't show up, or you can't manipulate them..
The problem arises when you try to access the "content" property of your "Loader" object. It throws a SecurityError.
The solution is quite simple.
Actionscript:
-
import flash.display.DisplayObject;
-
import flash.display.LoaderInfo;
-
import flash.system.LoaderContext;
-
import flash.display.Loader;
-
import flash.display.Sprite;
-
import flash.events.Event;
-
import flash.net.URLRequest;
-
-
...
-
-
// this is where we create the loader:
-
var loader:Loader = new Loader();
-
// this forces the loader to look for a crossdomain.xml file
-
var lctx:LoaderContext = new LoaderContext(true);
-
-
// listen for the init event on the loader
-
loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
-
// load the image, here the path is not valid.. i made it up
-
loader.load(new URLRequest("http://www.cssq.com/dunno.png", lctx);
-
-
....
-
-
-
// inside of the doImgInit function
-
protected function doImgInit(evt:Event):void
-
{
-
// get a reference to the LoaderInfo object in which the image is loaded
-
var lInfo:LoaderInfo = evt.target as LoaderInfo;
-
-
// this variable is used as reference to the image in the end.
-
var dO:DisplayObject;
-
-
// try to access the "content" property of the loader, if it works, there is a crossdomain.xml file.
-
try{
-
dO = lInfo.loader.content;
-
}
-
-
// if there wasn't one, we need to put the loader inside another object in order to manipulate it
-
catch(err:SecurityError)
-
{
-
// create a new Sprite to contain the loaded image
-
var sprt:Sprite = new Sprite();
-
// add the loader to the sprite
-
sprt.addChild(lInfo.loader);
-
// get a reference to this sprite in the dO variable
-
var dO:DisplayObject = sprt as DisplayObject;
-
}
-
-
// from here on you can do anything to the dO variable, rotate it, draw it unto a bitmapData, move it around..
-
// but first don't forget to add to to some container that is on the stage so you can see it!
-
}
Cheers!

just saw this… will try it out. if it works you are amazing !!!
March 14th, 2008 at 3:39 pm
This is the perfect solution… Thanks!
May 15th, 2008 at 6:14 pm
Works like a charm! Very smooth. Thanks!!
June 4th, 2008 at 8:45 pm
Is this a kind of hack?
the most important thing is it works!Thanks very much!!
July 29th, 2008 at 10:24 pm
Man, I owe you a couple of beers.
It’s fri evening, 20:21 and i’m still in the office trying to work around this problem. Yours is the only solution that actually worked!
High Five!!
January 30th, 2009 at 1:22 pm
It throws a sandbox security error when you try to draw dO to BitmapData. The image does load though!
February 2nd, 2009 at 11:00 pm
Works with in Flex as well. Thank you
February 12th, 2009 at 10:54 pm
Nice work!
I’m not sure, but I think this is loading the image ‘as content’, not ‘as data’. Which is why people are reporting they can see it, just can’t access the pixel data (i.e. when doing a draw to BitmapData). I could easily be wrong, but you can read more here:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d1b.html
May 27th, 2009 at 2:38 am
Thanks so much for posting this - I’ve been tearing my hair out for hours - your solution is that only one that works for me (picasa api + flashplayer 10)
June 9th, 2009 at 4:05 pm
This is great, thank you for posting this.
August 9th, 2009 at 9:37 pm
Damn this is awesome. I think you just saved my life.
I am eternally greatful.
October 8th, 2009 at 10:33 am
[...] couple of different things to try and get round this problem most notably Martin Legris’ fix (Getting around the crossdomain.xml file when loading images in as3) but annoyingly nothing I tried would [...]
November 9th, 2009 at 5:16 pm
even if there is a crossdomain.xml by default, or loaded with Security.loadPolicyFile, the try fails…
so using the code in the catch is ok…but when trying to draw the displayobject to a bitmapdata, a security error occurs again !
November 17th, 2009 at 8:06 am
My friend you made a mistake. You can manage the loader, stretch it and rotate, but you cant access bitmap data without crossdomain security. You can copy BitmapData or manage it ((
Nice try. Good idea for using the loader.
December 31st, 2009 at 6:45 am
[...] A workaround has been found that allows you to get around the issue and display the image on screen. The as3 workaround can be found here. [...]
March 5th, 2010 at 1:03 pm