<!--
{
  "documentType" : "article",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/creating-and-populating-buffers-from-core-graphics-images",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Creating and Populating Buffers from Core Graphics Images"
}
-->

# Creating and Populating Buffers from Core Graphics Images

Initialize vImage buffers from Core Graphics images.

## Discussion

[`vImage_Buffer`](/documentation/Accelerate/vImage_Buffer) structures are the basic data structures that vImage uses for working with images. They describe an image’s dimensions and contain the pixel data that vImage routines operate on.

Typically, you’ll initialize a source buffer from an image and initialize and allocate a destination buffer to receive the result of a vImage operation.

### Initialize a Source Buffer from a Core Graphics Image

The vImage functions that initialize a buffer’s size and data require an instantiated [`vImage_Buffer`](/documentation/Accelerate/vImage_Buffer) structure. Typically, you declare a buffer as a variable because these functions mutate the buffer.

You can initialize a vImage buffer from a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> instance that’s acquired from the <doc://com.apple.documentation/documentation/UIKit/UIImage/cgImage> property of an image. In the following example, the image is named `Flowers_2.jpg`. The [`init(cgImage:format:flags:)`](/documentation/Accelerate/vImage_Buffer/init(cgImage:format:flags:)) function initializes a [`vImage_Buffer`](/documentation/Accelerate/vImage_Buffer) structure with the image data using the format discussed in <doc://com.apple.documentation/documentation/Accelerate/converting-bitmap-data-between-core-graphics-images-and-vimage-buffers>.

```swift
guard
    let cgImage = UIImage(named: "Flowers_2.jpg")?.cgImage,
    var sourceBuffer = try? vImage_Buffer(cgImage: cgImage,
                                          format: format) else {
                                            return nil
}
```

### Initialize and Allocate a Destination Buffer

Typically, in addition to creating a buffer to represent your source image, you create a destination buffer to receive the result of the vImage operation. In this case, you use the [`init(width:height:bitsPerPixel:)`](/documentation/Accelerate/vImage_Buffer/init(width:height:bitsPerPixel:)) function to initialize a buffer of a specified size and the correct memory allocation for the bit-depth of the image:

```swift
guard var destinationBuffer = try? vImage_Buffer(width: Int(sourceBuffer.width),
                                                 height: Int(sourceBuffer.height),
                                                 bitsPerPixel: format.bitsPerPixel) else {
                                                    return nil
}
```

### Free the Buffer Memory

After you’re finished with buffers that have have their own memory allocation, it’s important that you free the memory allocated to them:

```swift
sourceBuffer.free()
destinationBuffer.free()
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)