ios - 尝试使用 Haxe 在 Mac/iOS 上保存图像数据时出错

标签 ios macos haxe

我正在创建一组将文本和图像保存到文件的 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 设备,而且我不确定我需要从测试人员那里获得哪些信息。

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

最佳答案

我的猜测是在 Bytes->String 转换过程中出现了问题,这在这里是完全没有必要的。我建议您删除它并使用 FileOutput 的二进制版本。如果您使用的是最新版本的 lime,您可以将 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);

关于ios - 尝试使用 Haxe 在 Mac/iOS 上保存图像数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752019/

相关文章:

ios - 自定义交互过渡动画

macos - SQLite 打不开——Meteor create app

function - 在 Haxe 中传递任意函数参数列表

ios - 如何为图表的图例添加背景颜色

ios - Xcode 6 中 IBOutlet 创建的新行为

c - OS X 上的 vsprintf : EXC_BAD_ACCESS

objective-c - OSX 应用程序的向后兼容性

exception - (Neko) "alc_cleanup: 1 device not closed"而不是错误消息

flash - 多语言闪存

ios - 在ARKit中设置ARCamera曝光时间