1
我创建了一组Haxe功能,可将文本和图像保存到文件中。这些功能在Windows和Android上运行良好;然而,测试人员告诉我,试图保存图像会在iOS和Mac上产生此错误:尝试使用Haxe在Mac/iOS上保存图像数据时出错
ERROR: Failure type not string @ ./File.cpp:123
下面是两个函数的代码。
public static function savePNG(path:String, image:BitmapData, ?whenDone:Bool->Void):Void {
if (path.substr(path.length - 4).toLowerCase() != ".png") { path += ".png"; }
// Flash: Not possible to save; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
if (whenDone != null)
whenDone(false);
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
if (whenDone != null)
whenDone(false);
// Windows, Mac, Linux, iOS, and Android: Use the "saveText" function with the converted file
#else
var b:ByteArray = image.encode("png", 1);
saveText(path, b.toString(), whenDone);
#end
}
public static function saveText(path:String, content:String, ?whenDone:Bool->Void):Void {
var success:Bool = true;
var path2:String = "";
path = "/assets/data/" + path;
var a:Array<String> = DataUtils.subfold(path);
// Flash or HTML5: Not possible to save; error out
#if (flash || js)
success = false;
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#else
trace("ERROR: File IO cannot be accessed on HTML5.");
#end
// iOS and Android: Attempt to save to the storage directory
#elseif mobile
if (!FileSystem.exists(SystemPath.userDirectory + "/" + a[0])) {
FileSystem.createDirectory(SystemPath.userDirectory + "/" + a[0]);
}
path2 = SystemPath.userDirectory + "/" + a[0] + "/" + a[1];
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
// Windows, Mac, and Linux: Save straight to the "assets/data/" folder
#else
if (!FileSystem.exists(FileSystem.fullPath(a[0]))) {
FileSystem.createDirectory(FileSystem.fullPath(a[0]));
}
path2 = FileSystem.fullPath(a[0] + "/" + a[1]);
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
#end
if (whenDone != null)
whenDone(success);
}
错误来自最后的trace("ERROR: " + e);
行。我会解决自己的问题,但我没有Mac或iOS设备,但我不确定测试仪需要什么信息。
底线:如果在这段代码中有一个明显的错误,该如何解决?如果不是,我需要询问哪些故障排除信息?
感谢您抽出宝贵时间来回答!我无法正常工作。不知何故,现在Windows上正在生成相同的错误。要明确,'(b:Bytes)'应该是'haxe.io.Bytes'对象,对吗? – ETHproductions
要解决这个问题:变量'b'应该是'haxe.io.Bytes',但是因为ByteArray只是'haxe.io.Bytes'的一个摘要,所以'(b:Bytes)'传递它是正确的。此外,你正在使用什么版本的库和haxe? –
我不是100%确定的,但我相信我使用的是OpenFL 3.3.8,lime 2.6.8和Haxe 3.2.0。 – ETHproductions