This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer
objects returned from an AVCaptureSession
to be processed later. After some experimentation I discovered that AVCaptureSession
must be reusing its CMSampleBuffer
references. When I try to keep more than 15 the session hangs. So I thought I would make copies of the sample buffers. But I can't seem to get it to work. Here is what I have written:
var allocator: Unmanaged<CFAllocator>! = CFAllocatorGetDefault()
var bufferCopy: UnsafeMutablePointer<CMSampleBuffer?>
let err = CMSampleBufferCreateCopy(allocator.takeRetainedValue(), sampleBuffer, bufferCopy)
if err == noErr {
bufferArray.append(bufferCopy.memory!)
} else {
NSLog("Failed to copy buffer. Error: \(err)")
}
This won't compile because it says that Variable 'bufferCopy' used before being initialized
. I've looked at many examples and they'll either compile and not work or they won't compile.
Anyone see what I'm doing wrong here?
You can simply pass a CMSampleBuffer?
variable (which, as an optional,
is implicitly initialized with nil
) as inout argument with
&
:
var bufferCopy : CMSampleBuffer?
let err = CMSampleBufferCreateCopy(kCFAllocatorDefault, buffer, &bufferCopy)
if err == noErr {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With