2007-06-12
AS3: Loading External Images
I really must say that Actionscript 3 is far far better and faster then all the previous versions and I like it very much. From my experience the Flash market, and especially the Flash 9 market, is growing extremly fast. I´ll get job offers nearly every week, sometimes even 2 at a day. So I have the freedom to pick out what I like most. Currently I am involved in a project where I am creating a Java-Backend driven Flash User Frontend. Part of the project is to load and display external Images from untrusted websites. So basically what Flash should do is loading the image and creating a thumbnail and a better and bigger version of the image which should display when the user moves the mouse over the thumbnail.
-
this.loader = new Loader();
-
this.loader.load("http://somewebsite.com/image.png");
-
this.loader.addEventListener(Event.COMPLETE, onLoadComplete);
-
-
public function onLoadComplete(event:Event):void {
-
var thumbnail:Sprite = new Sprite();
-
thumbnail.addChild(this.loader);
-
}
No problem so far! The image is available when onLoadComplete is thrown and the thumbnail gets displayed. Next I wanted to add the mouseEvent which displays the bigger version of the image. My idea was to copy the bitmapData of the loader object. So I did and had a Security Exception as a sideefffect because copying bitmap data of loaded image from an untrusted website (no crossdomain.xml) violates with the local sandbox.
-
/* throws Security Violation if the
-
loaded image came from an untrustes website */
-
var bigImage:Bitmap =
-
new Bitmap(loader.content).bitmapData.clone();
You might ofcourse download the image and display it but you are not allowed to even touch or manipulate the bitmap data, because you might inject the image with bad code which could lead to an unsafe system. This makes sense ofcourse. Now I know better.
So for the moment I created a new loader for the the image which displays on mouseOver Event because I could not find a better way (like cloning the loader object) without having Security problems again. I can live with this solution, but do you have any better idea?
23 Comments
-
would maybe php proxy thing help
http://www.abdulqabiz.com/blog/archives/general/php_proxy_script_for.php -
Yep, proxy is fine since we might load data from nearly everywhere, so we cannot ask everyone to add a crossdomain.xml to their serverx. This solution is ok, at least I now have one that will work. But still I would be interested how the pure Actionscript solution might look like. I believe it is possible to copy the loader object somehow. I just dont know how yet.
-
Very helpfull, Thx a lot for sharing.
-
The PHP proxy helped me immensely, I was running a script fine on my local box and when I pushed it up to my server, nothing was showing! I just wish it would have been cleared in the documentation. Here is an adobe doc with more info about the proxy:
http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_16520&sliceId=1
-
Hi,
sounds just what I need for a project I´m working on, somebody needs an XML driven gallery with thumbnails. All the images are stord on their own server, so no security issue, I guess. But what I wonder is this: When You create a thumbnail of something, it might be the display size that is thumbnail, but (at least that´s what I remember from AS2) the load size is whatever the image is, so the thumbnail in this case is not to have something that loads really fast and to only make the user wait longer in case he wants to see the bigger version, but rather to fit more pictures on the page, is that right? (Hence the bitmapdata copy).
I just wonder, because tht issue will come up, and the only workaround I can think of is to have every picture as a big and a small version on your server…
Anyways, maybe you have a thought on this and share it with me..
best
Mathias
