ios - Swift 中的 UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> 是什么?

标签 ios swift

我需要调用 CMSampleBufferCreateCopy 函数来创建 sampleBuffer 的副本,但我真的不知道如何使用它。

根据 this solution它应该像这样工作:

var bufferCopy: Unmanaged<CMSampleBuffer>!

let error = CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &bufferCopy)

但事实并非如此。

我得到的错误信息:

Cannot invoke 'CMSampleBufferCreateCopy' with an argument list of type '(CFAllocator!, CMSampleBuffer!, inout Unmanaged<CMSampleBuffer>!)'

编辑:

@availability(iOS, introduced=4.0)
func CMSampleBufferCreateCopy(allocator: CFAllocator!, sbuf: CMSampleBuffer!, sbufCopyOut: UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?>) -> OSStatus

/*! @param allocator
                                                        The allocator to use for allocating the CMSampleBuffer object.
                                                        Pass kCFAllocatorDefault to use the default allocator. */
/*! @param sbuf
                                                        CMSampleBuffer being copied. */
/*! @param sbufCopyOut
                                                        Returned newly created CMSampleBuffer. */

/*!
    @function   CMSampleBufferCreateCopyWithNewTiming
    @abstract   Creates a CMSampleBuffer with new timing information from another sample buffer.
    @discussion This emulates CMSampleBufferCreateCopy, but changes the timing.
                Array parameters (sampleTimingArray) should have only one element if that same
                element applies to all samples. All parameters are copied; on return, the caller can release them,
                free them, reuse them or whatever.  Any outputPresentationTimestamp that has been set on the original Buffer
                will not be copied because it is no longer relevant.    On return, the caller owns the returned 
                CMSampleBuffer, and must release it when done with it.

 */

最佳答案

从最后一个参数的声明可以看出,

sbufCopyOut: UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?>

bufferCopy 必须声明为可选,而不是隐式声明 展开可选:

var bufferCopy: Unmanaged<CMSampleBuffer>?

请注意,您必须对结果调用 takeRetainedValue(), 所以完整的解决方案是:

var unmanagedBufferCopy: Unmanaged<CMSampleBuffer>?
if CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &unmanagedBufferCopy) == noErr {
    let bufferCopy = unmanagedBufferCopy!.takeRetainedValue()
    // ...

} else {
    // failed
}

更新:在 Swift 4 中(可能已经在 Swift 4 中), CMSampleBufferCreateCopy() 返回一个托管对象,因此 代码简化为

var bufferCopy: CMSampleBuffer?
if CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &bufferCopy) == noErr {
    // ... use bufferCopy! ...
} else {
    // failed
}

关于ios - Swift 中的 UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31360488/

相关文章:

objective-c - 在 UIImage 中顺序移动方 block

swift - 为什么 SwiftyJSON 为其常量创建隐式解包可选?

ios - 动态调整单元格大小时出错

ios - 在 iOS 上的 Safari 共享扩展中获取 SLComposeServiceViewController 中的 URL

iphone - 如何在 iPhone 中加载高质量的 .gif 图像

iphone - 使用本地时区将 unix 时间戳转换为 NSdate

ios - 播放完成后如何自动关闭 AVPlayer?

ios - 在 Firebase 中,我可以从 .observeSingleEventOfType() 获取已完成执行其代码块的信号吗?

swift - 在 IF 语句中使用未解析的标识符

swift - 按日期排序 HealthKit 数据