2015-07-11 136 views
4

我需要調用CMSampleBufferCreateCopy函數來創建sampleBuffer的副本,但我無法真正弄清楚如何使用它。Swift中的UnsafeMutablePointer <Unmanaged <CMSampleBuffer>?>是什麼?

根據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. 

*/ 
+0

CMD-點擊'CMSampleBufferCreateCopy',讓我看看你會得到什麼 – Kametrixom

+0

用結果編輯問題。 @Kametrixom – zzzel

+1

如果將'Unmanaged !'更改爲'Unmanaged ,它可能會正常工作?'' – Kametrixom

回答

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 
} 
相關問題