0
votes

I'm currently loading a remote swf through as3 (with a domain policy to allow all from that domain - the policy file can't be changed by myself), that i want to take a snapshot of. The problem is that the main swf that i load, is loading child swf's from another domain that does not have the allow all policy turned on.. This makes the snapshot feature call a "sandbox security" error. I'm wondering if there's an alternative to the bitmapdata draw function to still take a "snapshot" without conflicting the sandbox security.

Thanks in advance.

3

3 Answers

2
votes

In case you are allowed to load data from the domain in question, do the following:

  1. load swf as binary data using URLLoader and URLLoaderDataFormat.BINARY
  2. load binary content into display object using Loader and loadBytes()

After that you should be able to draw() that swf's content.

EDIT Example of this technique.

Just copy and paste the code below into a blank fla, compile and place the resulting swf somewhere over http (when testing locally, both draw() calls will be a success, so you will not see the difference).

import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.system.Security;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.text.TextField;
import flash.display.StageScaleMode;

var onBinaryComplete:Function = function (event:Event) : void
{
    trace("onBinaryComplete()");
    var loader:URLLoader = event.target as URLLoader;
    var bytes:ByteArray = loader.data as ByteArray;
    trace(bytes.length+" bytes");
    bytesLoader.loadBytes(bytes);
}

var onBytesComplete:Function = function (event:Event) : void
{
    trace("onBytesComplete()");
    var info:LoaderInfo = event.target as LoaderInfo;
    var bmp:BitmapData = new BitmapData(300, 300, true, 0x8000FF00);
    // this will not fail, you'll see an image
    bmp.draw(info.content);
    var bitmap = new Bitmap(bmp);
    bitmap.x += 100;
    bitmap.y += 100;
    stage.addChild(bitmap); 
}

var onDirectLoadComplete:Function = function (event:Event) : void
{
    trace("onDirectLoadComplete()");
    var bmp:BitmapData = new BitmapData(300, 300, true, 0x80FF0000);
    // this must fail, you'll get an exception
    bmp.draw(event.target.content);
    stage.addChild(new Bitmap(bmp));
}

var binaryLoader:URLLoader = new URLLoader();
binaryLoader.dataFormat = URLLoaderDataFormat.BINARY;
binaryLoader.addEventListener(Event.COMPLETE, onBinaryComplete);
binaryLoader.load(new URLRequest("http://noregret.org/test/wrk.swf"));

var bytesLoader:Loader = new Loader();
bytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBytesComplete);

var directLoader:Loader = new Loader();
directLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onDirectLoadComplete);
directLoader.load(new URLRequest("http://noregret.org/test/wrk.swf"));

Loading of data crossdomain.xml: http://noregret.org/crossdomain.xml, but you have no control over loaded swf content when loading it directly. Loading bytes and then the content solves the problem.

1
votes

That's pretty much what the sandbox is for. So you can't make snapshots from data you don't own. This is a lame answer, but as the docs point out, the only way to get around it is by using AIR.

If you have access to the other domain or you know the people controlling it, you might be able to place a crossdomain.xml at their site.

0
votes

You can load the asset through some proxy (hosted on your own server). A lot of hosting companies don't allow hosting any proxy scripts, even if it's meant to load assets for applications.