我需要调用 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/