Swift 3:如何在48字节CFData中访问matrix_float3x3的值? [英] Swift 3: How to access the value of matrix_float3x3 in a 48-byte CFData?

查看:316
本文介绍了Swift 3:如何在48字节CFData中访问matrix_float3x3的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据此 answer 访问内在矩阵.

I'm trying to access intrinsic matrix following this answer.

通过运行以下命令,我可以得到48字节的 AnyObject ,然后将其进一步转换为 CFData .

By running the commend below, I was able to get a 48-byte AnyObject, and I further convert it into a CFData.

let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil)

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

However, I checked the output of sampleBuffer in CMSampleBuffer.h:

/*! @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取消引用指针.
  • Use the bridging from CFData to NSData to Data, and
  • the withUnsafeBytes method to get a pointer of the desired type to the data bytes,
  • .pointee to dereference the pointer.

示例:

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

    // ...

}

从上下文推断出指针类型(闭包内的$0) 为UnsafePointer<matrix_float3x3>.

The pointer type ($0 inside the closure) is inferred from the context as UnsafePointer<matrix_float3x3>.

这篇关于Swift 3:如何在48字节CFData中访问matrix_float3x3的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆