【问题标题】:Error when attempting to save image data on Mac/iOS with Haxe尝试使用 Haxe 在 Mac/iOS 上保存图像数据时出错
【发布时间】:2016-03-02 15:52:25
【问题描述】:

我正在创建一组将文本和图像保存到文件的 Haxe 函数。这些功能在 Windows 和 Android 上运行良好;但是,测试人员告诉我,尝试保存图像会在 iOS 和 Mac 上产生此错误:

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 设备,而且我不确定我需要测试人员提供哪些信息。

底线:如果此代码中有明显错误,如何修复?如果没有,我需要询问哪些故障排除信息?

【问题讨论】:

    标签: ios macos haxe


    【解决方案1】:

    我的猜测是在 Bytes->String 转换过程中出现了问题,这在此处完全没有必要。我建议您删除它并使用 FileOutput 的二进制版本。如果您使用的是最新版本的石灰,您可以将 ByteArray 转换为 Bytes(因为 ByteArray 基础类型是 Bytes)并将其写入 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);
    

    【讨论】:

    • 感谢您抽出宝贵时间回答!我无法让它发挥作用;不知何故,现在在 Windows 上产生了同样的错误。为了清楚起见,(b:Bytes) 应该是 haxe.io.Bytes 对象,对吗?
    • 澄清一下:变量b 应该是haxe.io.Bytes,但由于ByteArray 只是haxe.io.Bytes 的抽象,将其传递为(b:Bytes) 是正确的。另外,您使用的是什么版本的库和 haxe?
    • 我不是 100% 确定,但我相信我使用的是 OpenFL 3.3.8、lime 2.6.8 和 Haxe 3.2.0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多