在Swift中将字符串转换为小端字节?

问题描述

也许我不太了解小字节序,但我正在尝试完成一项似乎使我感到困惑的任务。我正在使用iOS应用程序中的点云,并试图实用地创建要导出的PLY文件。该应用程序运行正常,正在创建PLY文件并进行相应保存。但是,我发现可以更有效地保存写入文件的大量数据,因此建议考虑以 little endian 格式保存。

这是我当前的实现。当前所有值均为Floats;

private func writetoPly() {
    
    var filetoWrite = ""
    let headers = ["ply","format ascii 1.0","element vertex \(currentPointCount)","property float x","property float y","property float z","property uchar red","property uchar green","property uchar blue","property uchar alpha","element face 0","property list uchar int vertex_indices","end_header"]
    for header in headers {
        filetoWrite += header
        filetoWrite += "\r\n"
    }
    
    for i in 0..<currentPointCount {
        let point = particlesBuffer[i]
        let colors = point.color
        let red = colors.x * 255.0
        let green = colors.y * 255.0
        let blue = colors.z * 255.0
        let pvValue = "\(point.position.x) \(point.position.y) \(point.position.z) \(Int(red)) \(Int(green)) \(Int(blue)) 255"
        filetoWrite += pvValue
        filetoWrite += "\r\n"
    }

    let paths = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)
    let documentsDirectory = paths[0]
    let file = documentsDirectory.appendingPathComponent("ply_\(UUID().uuidString).ply")
    
    do {
        try filetoWrite.write(to: file,atomically: true,encoding: String.Encoding.ascii)
        self.fileWriteSubject.send(file)
    } catch {
        print("Failed to write PLY file",error)
    }
}

除了更改headers变量以指示文件将是 format binary_little_endian 1.0 格式外,我相信我还需要查看for in in 0..<currentPointCount循环,并确定如何处理将这些行转换为小端序格式,然后将其写入PLY文件(尽管我不确定我是否正确)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)