我需要调用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.
*/发布于 2015-07-11 19:20:35
从最后一个参数的声明中可以看出,
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
}更新: In Swift 4 (可能已经在Swift 4中),CMSampleBufferCreateCopy()返回托管对象,因此代码简化为
var bufferCopy: CMSampleBuffer?
if CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &bufferCopy) == noErr {
// ... use bufferCopy! ...
} else {
// failed
}https://stackoverflow.com/questions/31360488
复制相似问题