4 Replies Latest reply on Jul 18, 2008 1:02 AM by LU84

    Local image load and edit

      I already spend 2 days on this problem. I want to load local image into AIR application, edit and save as a new image. I know there are some security issues, which don't allow Loader class to access data on different domains than SWF file, but does it affect AIR too and local files editing? Well, sympthoms are the same.

      Here is the code:

      //pointing to an image
      var imageFile:File = File.documentsDirectory.resolvePath('someimage.png');

      //new file stream
      var fileStream: FileStream = new FileStream();
      fileStream.open(imageFile, FileMode.READ);

      //reading image into bytearray and closing stream
      var imgBytes: ByteArray = new ByteArray();

      //tried to use LoaderContext and some workarounds available in the web - none is working
      //var loaderContext: LoaderContext = new LoaderContext();
      //loaderContext.allowLoadBytesCodeExecution = true;

      //creating loader and injecting image bytes into it
      var loader:Loader = new Loader();
      loader.loadBytes( imgBytes, loaderContext );
      //trace(loader.content); // will give you null

      //creating a sprite and adding image to it
      var somesprite:Sprite = new Sprite();

      //creating output file
      var myFile:File = File.documentsDirectory.resolvePath('newimage.png');
      var myFileStream:FileStream = new FileStream();
      myFileStream.openAsync(myFile, FileMode.WRITE);

      //Here is fun part: BitmapData capture everything in sprite except image
      var bd:BitmapData=new BitmapData(200,200);
      var ba:ByteArray = PNGEncoder.encode(bd);

      //However image will visible in the screen

      Maybe somebody already noticed that issue and know the solution? It's not very sophisticated example - it won't let you do even a simple image editor. I'm sure there must be some way.
      Thanks in advance