问题
Flash Player supported Thread in 11.5+.
I want to load an image by using Loader.loadBytes() in Worker Thread.
Which the Image ByteArray are generated in Main Thread.
But I can NOT do it.
I got a SecurityError like this:
SecurityError: Error #2123: Security sandbox violation: Loader.content: file:///E:/work/ASWorkSpace/test/bin-debug/test.swf cannot access file:///E:/work/ASWorkSpace/test/bin-debug/test.swf/[[DYNAMIC]]/1. No policy files granted access.
I init my worker thread like:
worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
so it's not a swf from remote, but it's local or like
[Embed(source="../workerswfs/Thread.swf", mimeType="application/octet-stream")]
I found in manual. It said "If the loaded content is an image, its data cannot be accessed by a SWF file outside of the security sandbox, unless the domain of that SWF file was included in a URL policy file at the origin domain of the image."
I have gotten a solution. I loadByte() twice, it seems "washed off" the ByteArray source. so FlashPlayer considered the ByteArray generated by Worker Thread, and it allowed to access loader.content. Like this:
//Messages to the Main thread
protected function onMainToWorker(event:Event):void {
var msg:ByteArray = mainToWorker.receive() as ByteArray;
trace(msg.length);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bytesComplete);
var bytes:ByteArray = new ByteArray();
bytes.writeBytes(msg,0,msg.length);
loader.loadBytes(bytes);
}
private function loadedAagin(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
// Here we can access it.
var bmp:Bitmap = loader.content as Bitmap;
}
private function bytesComplete(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, bytesComplete);
// var bmp:Bitmap = loader.content as Bitmap; It can not be accessed
// just loadBytes again, it seems "washed off" the ByteArray source
var newloader:Loader = new Loader();
newloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedAagin);
newloader.loadBytes(loader.contentLoaderInfo.bytes);
}
But it's so ugly isn't it? Is there anyone has good idea?
来源:https://stackoverflow.com/questions/16889490/as3-can-not-load-a-bitmap-by-using-loadbyte-in-thread