Skip to content
This repository was archived by the owner on May 15, 2022. It is now read-only.

Shape Export to Objective C

claus edited this page Aug 13, 2010 · 21 revisions

With as3swf you can automatically generate Objective-C source code (Core Graphics, Quartz) from SWF Shapes, to reuse those shapes in your iPhone projects. The CoreGraphics exporter generates header (.h) and class (.m) files.


Example

The following example is a simple AIR application that loads a SWF and exports all contained shapes to Objective-C source code ready to use in iPhone projects. Source files are written to a folder “as3swf” on the Desktop.

package 
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.filesystem.File;
  import flash.filesystem.FileMode;
  import flash.filesystem.FileStream;
  import flash.net.URLLoader;
  import flash.net.URLLoaderDataFormat;
  import flash.net.URLRequest;
  import flash.utils.ByteArray;
  
  import com.codeazur.as3swf.SWF;
  import com.codeazur.as3swf.tags.ITag;
  import com.codeazur.as3swf.tags.TagDefineShape;
  import com.codeazur.as3swf.exporters.CoreGraphicsShapeExportDocumentHandler;

  public class Test extends Sprite
  {
    public function Test()
    {
      var request:URLRequest = new URLRequest("any.swf");
      var loader:URLLoader = new URLLoader();
      loader.dataFormat = URLLoaderDataFormat.BINARY;
      loader.addEventListener(Event.COMPLETE, completeHandler);
      loader.load(request);
    }

    private function completeHandler(e:Event):void {
      var swf:SWF = new SWF(URLLoader(e.target).data as ByteArray);
      for (var i:uint = 0; i < swf.tags.length; i++) {
        var tag:ITag = swf.tags[i];
        if (tag is TagDefineShape) {
          var defineShape:TagDefineShape = tag as TagDefineShape;
          var docHandler:CoreGraphicsShapeExportDocumentHandler = 
            new CoreGraphicsShapeExportDocumentHandler(
              "Shape" + defineShape.shapeId + "View",
              "Claus Wahlers",
              "côdeazur brasil"
            );
          defineShape.export(docHandler);
          writeSource(docHandler);
        }
      }
    }

    private function writeSource(docHandler:CoreGraphicsShapeExportDocumentHandler):void {
      var fileName:String = "as3swf" + File.separator + docHandler.className;
      var h:File = File.desktopDirectory.resolvePath(fileName + ".h");
      var m:File = File.desktopDirectory.resolvePath(fileName + ".m");
      var stream:FileStream = new FileStream();
      stream.open(h, FileMode.WRITE);
      stream.writeUTFBytes(docHandler.h);
      stream.close();
      stream.open(m, FileMode.WRITE);
      stream.writeUTFBytes(docHandler.m);
      stream.close();
    }
  }
}

A Simple Demo

Here’s a simple demo of a SWF whose shapes get exported to Objective-C:

“This SWF”: http://codeazur.com.br/stuff/as3swf/iphone/codeazur_logo.swf contains only one shape (the côdeazur logo).

Exported to Objective-C we get the following source files:


The final result, integrated in a simple iPhone project:

Clone this wiki locally