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