Quite a while ago Steven Sacks posted a solution to 300 redirects when loading images in Flash.
This seemed to work most of the time but the solution does not work every time (at least anymore).
One thing I noticed with Facebook in particular is that the contentLoaderInfo.url (the redirected url) was not a complete url with profile images. So loading that url would not work.
Started looking into other possible solutions and low-and-behold, the language reference actually tells you what to do.
At the bottom of the checkPolicyFile section, Adobe mentions steps to take with redirects:
- Examine the value of LoaderInfo.url after you have received a ProgressEvent.PROGRESS or Event.COMPLETE event, which tells you the object’s final URL.
- Call the Security.loadPolicyFile() method with a policy file URL based on the object’s final URL.
- Poll the value of LoaderInfo.childAllowsParent until it becomes true.
I put together some example source code below to walk you through the steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
private var loader:Loader; private var accessChildTimer:Timer; private var url:String; private function init():void { accessChildTimer = new Timer( 100, 100 ); accessChildTimer.addEventListener( TimerEvent.TIMER, onAccessChildTimer ); } public function loadImage():void { loader = new Loader(); loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaderComplete ); var context:LoaderContext = new LoaderContext(); context.checkPolicyFile = true; var request:URLRequest = new URLRequest( url ); loader.load( request, context ); } protected function onLoaderComplete( event:Event ):void { var loaderInfo:LoaderInfo = loader.contentLoaderInfo; if( loaderInfo.childAllowsParent ) { displayImage(); } else if( loaderInfo.url != this.url ) { var redirectURL:String = loaderInfo.url + "crossdomain.xml"; //Might want to write a method to append a '/' if necessary. Security.loadPolicyFile( redirectURL ); accessChildTimer.reset(); accessChildTimer.start(); } } protected function onAccessChildTimer( event:TimerEvent ):void { var loaderInfo:LoaderInfo = loader.contentLoaderInfo; if( loaderInfo.childAllowsParent ) { accessChildTimer.stop(); displayImage(); } } private function displayImage():void { //Access BitmapData here } |
Hopefully that helps you out!
FYI,
The timer will run indefinitely while “childAllowsParent == false”.
The
function onAccessChildTimer( )
should use a maxCount [for each 100ms iteration] before it “kills” the timer and request; perhaps throw an exception.Oops. Sorry I see you have a maxCount == 100.
My bad!