2016-03-02 34 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

相关问题