swiftpointerscgimage

Swift: Initialize UnsafeMutablePointer<CGImage?> without allocating


I want to call this function: VTCreateCGImageFromCVPixelBuffer(imageBuffer, options: nil, imageOut: cgImageColorPtr)

The third argument is an UnsafeMutablePointer<CGImage?>. I want to initialize an UnsafeMutablePointer<CGImage?> and then pass it to this function. I'm not sure how to do that without invoking UnsafeMutablePointer<CGImage?>.allocate(capacity:). I don't want to do that because, according to the documentation, VTCreateCGImageFromCVPixelBuffer(imageBuffer, options: nil, imageOut: cgImageColorPtr) does the allocation.

What is the simplest way to initialize an UnsafeMutablePointer<CGImage?>?


Solution

  • Just declare a var of type CGImage?, and pass it with the & prefix.

    var cgImageColor: CGImage? = nil
    
    VTCreateCGImageFromCVPixelBuffer(imageBuffer, options: nil, imageOut: &cgImageColor)