守望者--AIR技术交流
标题:
用Embed引入的swf资源,取出里面的子资源
[打印本页]
作者:
破晓
时间:
2015-1-8 10:02
标题:
用Embed引入的swf资源,取出里面的子资源
以前取出用Embed嵌入的资源都用这样的方法:
[Embed(source="assets/project.swf", symbol="image1")]
public var img:Class;
复制代码
然后实例化使用,今天打断点发现用Embed嵌入的资源运行时进入了MovieClipLoaderAsset类,看一下其构造方法:
public function MovieClipLoaderAsset()
{
super(); var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = new
ApplicationDomain(ApplicationDomain.currentDomain);
// in AIR...
// when embedding a SWF using @Embed, you are actively asking for the SWF
// to be executed, otherwise the SWF will fail loading due to
// Loader.allowLoadBytesCodeExecution.
//
// this property prevents accidentally loading a potentially dangerous
// SWF into the application sandbox.
//
// since this property is indirectly accessed, this should be revisited
// after AIR 1.x, as it may become deprecated
if ("allowLoadBytesCodeExecution" in loaderContext)
loaderContext["allowLoadBytesCodeExecution"] = true; loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
completeHandler);
loader.loadBytes(movieClipData, loaderContext);
addChild(loader);
}
复制代码
发现添加了一个loader,于是发现只要把loader取出来就可以了
[Embed(source="assets/project.swf")]
public var swf:Class;
private function onLoadComplete(event:Event):void
{
var cls:Class = event.target.applicationDomain.getDefinition("image1") as Class;
var bmp:BitmapData = new cls();
}
private function init():void
{
var c:* = new swf();
(c.getChildAt(0) as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
}
复制代码
取出图片成功
不过Embed的方式其实效率并不是太高,而且还增加主工程的大小,所以还是建议用loader来加载资源
欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)