从原始表中提取值

时间:2018-10-22 16:35:51

标签: swift

我得到一个表lensDistortionLookupTable,它是AVCalibrationData的浮点数的NSData,并想将其转换为数组。那是什么 用Swift做正确的方法吗?

1 个答案:

答案 0 :(得分:1)

  1. 使用DatawithUnsafeBytes中获取字节
  2. 使用它创建UnsafeBufferPointer中的Float32(根据文档)
  3. 将其投射到常规Array

    let float32size = MemoryLayout<Float32>.stride // should be 4, but do not hardcode
    let elementCount = data.count / float32Size
    
    let table: [Float32] = depthData.cameraCalibrationData!.lensDistortionLookupTable.withUnsafeBytes {
                       return Array(UnsafeBufferPointer<Float32>(start: $0, count: elementCount))
                    }