Article

Creating a Core Graphics Image from a vImage Buffer

Create displayable representations of vImage buffers.

Overview

vImage provides a function for creating Core Graphics images from vImage buffers. This function allows you to display the results of a vImage operation to your user.

Create the Image

You create a Core Graphics image from the buffer, and initialize a UIImage instance from that. The vImageCreateCGImageFromBuffer(_:_:_:_:_:_:) function returns an unmanaged CGImage instance based on the supplied buffer and Core Graphics image format (for more information, see Creating a Core Graphics Image Format).

The following example shows how to create a Core Graphics image from a vImage buffer.

let cgImage = vImageCreateCGImageFromBuffer(
    &destinationBuffer,
    &format,
    nil,
    nil,
    vImage_Flags(kvImageNoFlags),
    &error)

You use takeRetainedValue() to acquire a managed reference to the unmanaged CGImage instance that you pass to the UIImage initializer.

if let cgImage = cgImage {
    // Assumes `imageView` is a `UIImageView`
    imageView.image = UIImage(cgImage: cgImage.takeRetainedValue())
}

See Also

First Steps

Creating a Core Graphics Image Format

Provide descriptions of Core Graphics image formats for conversions to and from vImage.

Creating and Populating Buffers from Core Graphics Images

Initialize vImage buffers from Core Graphics images.