2016-03-02 68 views
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設備,但我不確定測試儀需要什麼信息。

底線:如果在這段代碼中有一個明顯的錯誤,該如何解決?如果不是,我需要詢問哪些故障排除信息?

回答

1

我的猜測是在Bytes-> String轉換過程中有些東西搞亂了,這在這裏完全沒有必要。我建議你刪除它並使用FileOutput的二進制版本。如果您正在使用最新版本的石灰,則可以將ByteArray轉換爲字節(因爲ByteArray基礎類型爲字節)並將其寫入FileOutput。這裏是「寫」部分應該怎麼看起來像

/** 
* Save bytes as a file 
* @param path  
* @param content 
* @return true if succeed, false overwise 
*/ 
static function saveBytes(path:String, content:Bytes):Bool 
{ 
    var success = false; 
    var fo:FileOutput = null; 
    try { 
     //open binary file and write bytes 
     fo = File.write(path, true);  
     fo.writeBytes(content, 0, content.length);   
     success = true; 
    } catch (e:Dynamic) { 
     trace("ERROR: " + e); 
     errorify(e); 
    } 

    //file output should be closed in any case 
    try { 
     if (fo != null) 
      fo.close(); 
    } catch (e:Dynamic) { 
     trace("ERROR: " + e); 
     errorify(e); 
    } 

    return success; 

} 

此外,編碼部分

/* insert your path construction here */ 
var b:ByteArray = image.encode("png", 1); 
var success = saveBytes(path, (b:Bytes)); 
if (whenDone != null) 
    whenDone(success); 
+0

感謝您抽出寶貴時間來回答!我無法正常工作。不知何故,現在Windows上正在生成相同的錯誤。要明確,'(b:Bytes)'應該是'haxe.io.Bytes'對象,對嗎? – ETHproductions

+0

要解決這個問題:變量'b'應該是'haxe.io.Bytes',但是因爲ByteArray只是'haxe.io.Bytes'的一個摘要,所以'(b:Bytes)'傳遞它是正確的。此外,你正在使用什麼版本的庫和haxe? –

+0

我不是100%確定的,但我相信我使用的是OpenFL 3.3.8,lime 2.6.8和Haxe 3.2.0。 – ETHproductions

相關問題