守望者--AIR技术交流

标题: 用Embed引入的swf资源,取出里面的子资源 [打印本页]

作者: 破晓    时间: 2015-1-8 10:02
标题: 用Embed引入的swf资源,取出里面的子资源
以前取出用Embed嵌入的资源都用这样的方法:
  1. [Embed(source="assets/project.swf", symbol="image1")]
  2. public var img:Class;
复制代码
然后实例化使用,今天打断点发现用Embed嵌入的资源运行时进入了MovieClipLoaderAsset类,看一下其构造方法:
  1. public function MovieClipLoaderAsset()
  2. {
  3.   super();  var loaderContext:LoaderContext = new LoaderContext();
  4.   loaderContext.applicationDomain = new
  5.    ApplicationDomain(ApplicationDomain.currentDomain);
  6.   
  7.   // in AIR...
  8.   // when embedding a SWF using @Embed, you are actively asking for the SWF
  9.   // to be executed, otherwise the SWF will fail loading due to
  10.   // Loader.allowLoadBytesCodeExecution.
  11.   //
  12.   // this property prevents accidentally loading a potentially dangerous
  13.   // SWF into the application sandbox.
  14.   //
  15.   // since this property is indirectly accessed, this should be revisited
  16.   // after AIR 1.x, as it may become deprecated
  17.   if ("allowLoadBytesCodeExecution" in loaderContext)
  18.    loaderContext["allowLoadBytesCodeExecution"] = true;  loader = new Loader();
  19.   loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
  20.               completeHandler);
  21.   loader.loadBytes(movieClipData, loaderContext);
  22.   addChild(loader);
  23. }
复制代码
发现添加了一个loader,于是发现只要把loader取出来就可以了
  1.                         [Embed(source="assets/project.swf")]
  2.                         public var swf:Class;
  3.                         
  4.                         private function onLoadComplete(event:Event):void
  5.                         {
  6.                                     var cls:Class = event.target.applicationDomain.getDefinition("image1") as Class;
  7.                                     var bmp:BitmapData = new cls();
  8.                         }
  9.                         
  10.                         private function init():void
  11.                         {
  12.                                     var c:* = new swf();
  13.                                     (c.getChildAt(0) as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  14. }
复制代码
取出图片成功

不过Embed的方式其实效率并不是太高,而且还增加主工程的大小,所以还是建议用loader来加载资源





欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)