I used to be able to save png files I generated in openfl, and with the snippet attached you still can when you use -Dlegacy
I tried to update the code to the ‘new’ openfl, but I run into something strange (perhaps an bug?)
Try to test the code without -Dlegacy
lime test neko
I get this error:
Main.hx:50: Error writing file /path/to/your/Desktop/test.png: Invalid field access : length
Which basically is: bytearray doesn’t have length
http://docs.openfl.org/lime/utils/ByteArray.html#length
(documentation agrees!!)
but what would work???
import openfl.display.Sprite;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.PNGEncoderOptions;
import openfl.utils.ByteArray;
import openfl.geom.Rectangle;
import haxe.io.Output;
import sys.io.FileOutput;
class Main extends Sprite {
public function new ()
{
super ();
var _bit = new openfl.display.Bitmap(Assets.getBitmapData( 'assets/robot.jpg'));
this.addChild(_bit);
saveImage(_bit.bitmapData,"/path/to/your/Desktop/test.png");
}
public function saveImage(image:BitmapData, outputFile:String):Void
{
#if (cpp || neko)
var path;
// path = Sys.getCwd();
// path = path.substr(0, path.indexOf('Export')) + "test2.png";
path = outputFile;
#if openfl_legacy
var imageData:ByteArray = image.encode('png', 1);
#else
var imageData:ByteArray = image.encode(new Rectangle(), PNGEncoderOptions);
#end
var fo:FileOutput = sys.io.File.write(path, true);
try {
fo.writeBytes(imageData, 0, imageData.length );
#if openfl_legacy
// fo.writeBytes(imageData, 0, imageData.length );
#else
// fo.writeBytes(imageData, 0, imageData.length );
#end
trace( "save path done: " + path );
} catch (e:Dynamic){
trace("Error writing file " + path + ": " + e);
}
fo.close();
#end
}
}