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

    Local image load and edit

    LU84
      Hello,
      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();
      fileStream.readBytes(imgBytes);
      fileStream.close();

      //tried to use LoaderContext and some workarounds available in the web - none is working
      //var loaderContext: LoaderContext = new LoaderContext();
      //loaderContext.checkPolicyFile=true;
      //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();
      somesprite.addChild(loader);

      //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);
      bd.draw(someSprite);
      var ba:ByteArray = PNGEncoder.encode(bd);
      myFileStream.writeBytes(ba);
      myFileStream.close();

      //However image will visible in the screen
      root.addChild(someSprite);


      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