What I'd like to do is provide a CVPixelBuffer as the dataInfo argument to CGDataProviderCreateWithData that has an initializer:
init?(dataInfo info: UnsafeMutableRawPointer?,
data: UnsafeRawPointer,
size: Int,
releaseData: @escaping CGDataProviderReleaseDataCallback)
My best (and probably wrong) approach to convert to a UnsafeRawPointer is:
let pixelBuffer: CVPixelBuffer
...
let ptr: UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating: &pixelBuffer)
However, the releaseData callback function is defined as:
typealias CGDataProviderReleaseDataCallback = (UnsafeMutableRawPointer?, UnsafeRawPointer, Int) -> Void
I cannot think of any way to get the CVPixelBuffer back from the UnsafeMutableRawPointer.
Clearly I need help!
Either I’ve completely misread this thread or you folks are working way too hard (-:
My reading of this thread is that you’re trying to associate a Swift object with a C object via that C object’s ‘info’ pointer (info, cookie, refCon, whatever). If so, the correct approach is to use Unmanaged
and OpaquePointer
:
-
You use
Unmanaged.passRetained(_:)
to get a retainedUnmanaged
reference to your Swift Object -
You convert that to
OpaquePointer
usingtoOpaque()
, which you can then pass to the ‘info’ pointer of the C API -
You use
Unmanaged.fromOpaque(_:)
to get back to yourUnmanaged
reference from that ‘info’ pointer -
You use either
takeRetainedValue()
ortakeUnretainedValue()
to get back to the original Swift object, depending on whether you want to release your unmanaged reference or not -
If you use
takeUnretainedValue()
, you can later callrelease()
to clean up
You can see an end-to-end example of this technique in this thread.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"