8

In objective-c you can easily cast a CVImageBufferRef as a CVPixelBufferRef:

CMSampleBufferRef sampleBuffer = some value

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

How can I do this in swift?


var pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)

gives the error: 'Unmanaged<CVPixelBufferRef>' is not convertible to 'CVPixelBufferRef'


var pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer).takeRetainedValue()

gives the error: 'CVImageBuffer' is not convertible to 'CVPixelBufferRef'


var pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer).takeRetainedValue() as CVPixelBufferRef

gives the error: Cannot convert the expression's type 'CVPixelBufferRef' to type 'CVPixelBufferRef'

3 Answers 3

17

EDIT

This answer was given during Swift beta test period. It seems that now the solution is simpler, as suggested by klinger

var pixelBuffer : CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)

however I'll leave the previous answer for historycal reasons :-)

PREVIOUS ANSWER

Look at the prerelease docs:

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-XID_40

Specifically this statement

Remapped Types

When Swift imports Core Foundation types, the compiler remaps the names of these types. The compiler removes Ref from the end of each type name because all Swift classes are reference types, therefore the suffix is redundant.

The Core Foundation CFTypeRef type completely remaps to the AnyObject type. Wherever you would use CFTypeRef, you should now use AnyObject in your code.

The first thing you would like to do, is to remove the "ref" from each type. However it's not necessary, since the "refs" are typealiased to the "non-ref" types.

Then, this statement should work. It may need some tuning before I've never worked with CMSampleBufferGetImageBuffer, and for this reason I'm not sure about the first line (initialize the buffer)

    var buf : CMSampleBuffer = // initialize the buffer
    var anUnmanaged : Unmanaged<CVImageBuffer> = CMSampleBufferGetImageBuffer(buf)
    var returnValue = anUnmanaged.takeUnretainedValue()

Or, shortly

    var buf : CMSampleBuffer = // initialize the buffer
    var anUnmanaged : CVImageBuffer = CMSampleBufferGetImageBuffer(buf).takeRetainedValue()

However, you asked for a CVPixelBuffer. If the two types are fully compatible (I don't know the underlying API so I assume that casting between CVPixelBuffer and CVImageBuffer in objc is always safe), there is no "automatism" to do it, you have to pass through an Unsafe Pointer.

The complete code is this:

    var buf : CMSampleBuffer = // initialize the buffer
    var anUnmanaged : Unmanaged<CVImageBuffer> = CMSampleBufferGetImageBuffer(buf)
    var returnValue = anUnmanaged.takeUnretainedValue()

    var anOpaque = anUnmanaged.toOpaque()
    var pixelBuffer : CVPixelBuffer = Unmanaged<CVPixelBuffer>.fromOpaque(anOpaque).takeUnretainedValue()

I used takeUnretainedValue() that doesn't consume a retain count, since CMSampleBufferGetImageBuffer() returns an unretained object

2
  • 1
    Thank you! In objective-c CVImageBufferRef is a synonym for a CVPixelBufferRef, so this was driving me crazy.
    – klinger
    Jun 24, 2014 at 16:05
  • In this case I suggest you to file a bug-feature request to have an automatic cast in a future release :-) I don't exclude that there is some function to bridge between the two types hidden somewhere, however passing through an opaque pointer is the first solution that came to my mind :-)
    – LombaX
    Jun 24, 2014 at 16:09
5

In Xcode 6 beta 6 this finally works:

var pixelBuffer : CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)

1

@LombaX's solution didn't work for me in the latest build, Xcode Beta5. Here's what did work:

let imageBuffer : CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let srcPtr = Unmanaged.passUnretained(imageBuffer).toOpaque()
let pixelBuffer = Unmanaged<CVPixelBuffer>.fromOpaque(srcPtr).takeUnretainedValue()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.