什么是UnsafeMutablePointer <unmanaged <cmsamplebuffer>?&gt;在斯威夫特?

时间:2015-07-11 18:35:03

标签: 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.

 */

1 个答案:

答案 0 :(得分:4)

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

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
}