Getting around the crossdomain.xml file when loading images in as3


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import flash.display.DisplayObject;
  2. import flash.display.LoaderInfo;
  3. import flash.system.LoaderContext;
  4. import flash.display.Loader;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.net.URLRequest;
  8. ...
  9. // this is where we create the loader:
  10. var loader:Loader = new Loader();
  11. // this forces the loader to look for a crossdomain.xml file
  12. var lctx:LoaderContext = new LoaderContext(true);
  13. // listen for the init event on the loader
  14. loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
  15. // load the image, here the path is not valid.. i made it up
  16. loader.load(new URLRequest("http://www.cssq.com/dunno.png", lctx);
  17. ....
  18. // inside of the doImgInit function
  19. protected function doImgInit(evt:Event):void
  20. {
  21. // get a reference to the LoaderInfo object in which the image is loaded
  22. var lInfo:LoaderInfo = evt.target as LoaderInfo;
  23. // this variable is used as reference to the image in the end.
  24. var dO:DisplayObject;
  25. // try to access the "content" property of the loader, if it works, there is a crossdomain.xml file.
  26. try{
  27. dO = lInfo.loader.content;
  28. }
  29. // if there wasn't one, we need to put the loader inside another object in order to manipulate it
  30. catch(err:SecurityError)
  31. {
  32. // create a new Sprite to contain the loaded image
  33. var sprt:Sprite = new Sprite();
  34. // add the loader to the sprite
  35. sprt.addChild(lInfo.loader);
  36. // get a reference to this sprite in the dO variable
  37. var dO:DisplayObject = sprt as DisplayObject;
  38. }
  39. // from here on you can do anything to the dO variable, rotate it, draw it unto a bitmapData, move it around..
  40. // but first don't forget to add to to some container that is on the stage so you can see it!
  41. }

URL: http://blog.martinlegris.com/?p=82#more-82

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.