Swift 3:如何访问一个48字节的CFData中matrix_float3x3的值?

Swift 3: How to access the value of matrix_float3x3 in a 48-byte CFData?

我正在尝试访问此 之后的内在矩阵。

通过运行下面的推荐,我得到了一个48字节的AnyObject, and I further convert it into a CFData

let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil)

但是,我检查了 CMSampleBuffer.h 中 sampleBuffer 的输出:

/*! @constant   kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix
     @abstract  Indicates the 3x3 camera intrinsic matrix applied to the current sample buffer.
     @discussion Camera intrinsic matrix is a CFData containing a matrix_float3x3, which is column-major.
        ....
 */
CM_EXPORT const CFStringRef kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix  // CFData (matrix_float3x3) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_11_0);

我应该如何从 CFData 访问 matrix_float3x3 中的值?

这应该有效:

  • 使用从CFDataNSDataData的桥接,以及
  • withUnsafeBytes方法获取所需的指针 输入数据字节,
  • .pointee 取消引用指针。

示例:

if let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) as? Data {
    let matrix: matrix_float3x3 = camData.withUnsafeBytes { [=10=].pointee }

    // ...

}

指针类型(闭包内的[=16=])是从上下文中推断出来的 作为 UnsafePointer<matrix_float3x3>.

这是 Martin R 对 objective-c 的回答版本:

CFTypeRef cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil);
if(cameraIntrinsicData != nil){
    CFDataRef cfdr = (CFDataRef)(cameraIntrinsicData);
    matrix_float3x3 camMatrix = (matrix_float3x3 *)(CFDataGetBytePtr(cfdr));
}