<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 7.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vImageBuffer_InitWithCGImage(_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@vImageBuffer_InitWithCGImage"
  },
  "title" : "vImageBuffer_InitWithCGImage(_:_:_:_:_:)"
}
-->

# vImageBuffer_InitWithCGImage(_:_:_:_:_:)

Initializes a vImage buffer with the contents of a Core Graphics image.

```
func vImageBuffer_InitWithCGImage(_ buf: UnsafeMutablePointer<vImage_Buffer>, _ format: UnsafeMutablePointer<vImage_CGImageFormat>, _ backgroundColor: UnsafePointer<CGFloat>!, _ image: CGImage, _ flags: vImage_Flags) -> vImage_Error
```

## Parameters

`buf`

The destination vImage buffer. On output, an initialized buffer with all fields populated.

`format`

A [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) structure. Pass an empty structure to specify that the function populates the format with the properties of the Core Graphics image. Pass a populated structure to specify that the function converts the Core Graphics image to the format.

`backgroundColor`

If the source image contains alpha information and the format doesn’t contain alpha information, this function flattens the source image against this parameter.

`image`

The source Core Graphics image.

`flags`

The options to use when performing the operation. Pass [`kvImageNoAllocate`](/documentation/Accelerate/kvImageNoAllocate) if the destination buffer references existing data; otherwise, pass [`kvImageNoFlags`](/documentation/Accelerate/kvImageNoFlags).

## Return Value

[`kvImageNoError`](/documentation/Accelerate/kvImageNoError); otherwise, one of the error codes in <doc://com.apple.documentation/documentation/Accelerate/data-types-and-constants>.

## Discussion

The following code shows a passthrough function that accepts a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> image, populates a vImage buffer from the image, and generates a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> image from the buffer.

In this example, the call to [`vImageBuffer_InitWithCGImage(_:_:_:_:_:)`](/documentation/Accelerate/vImageBuffer_InitWithCGImage(_:_:_:_:_:)) populates the [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) and the [`vImage_Buffer`](/documentation/Accelerate/vImage_Buffer) variables with the properties of the source image:

```swift
static func passThrough(sourceImage: CGImage) -> CGImage? {

    var format = vImage_CGImageFormat()
    var buffer = vImage_Buffer()
    
    defer {
        buffer.free()
    }

    vImageBuffer_InitWithCGImage(
        &buffer,
        &format,
        nil,
        sourceImage,
        vImage_Flags(kvImageNoFlags))
   
    // Perform image-processing operations on `buffer`.

    let destinationCGImage = vImageCreateCGImageFromBuffer(
        &buffer,
        &format,
        nil,
        nil,
        vImage_Flags(kvImageNoFlags),
        nil)
    
    return destinationCGImage?.takeRetainedValue()
}
```

Pass a fully initialized [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) to specify that [`vImageBuffer_InitWithCGImage(_:_:_:_:_:)`](/documentation/Accelerate/vImageBuffer_InitWithCGImage(_:_:_:_:_:)) converts the source <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> image to the format that `format` describes. The following example converts the source image to a three-channel, 8-bit-per-channel RGB image:

```swift
static func passThrough(sourceImage: CGImage) -> CGImage? {
    
    var format = vImage_CGImageFormat(
        bitsPerComponent: 8,
        bitsPerPixel: 8 * 3,
        colorSpace: CGColorSpaceCreateDeviceRGB(),
        bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
        renderingIntent: .defaultIntent)!

    var buffer = vImage_Buffer()

    defer {
        buffer.free()
    }
    
    vImageBuffer_InitWithCGImage(
        &buffer,
        &format,
        nil,
        sourceImage,
        vImage_Flags(kvImageNoFlags))
    
    // Perform image-processing operations on RGB888 `buffer`.

    let destinationCGImage = vImageCreateCGImageFromBuffer(
        &buffer,
        &format,
        nil,
        nil,
        vImage_Flags(kvImageNoFlags),
        nil)
    
    return destinationCGImage?.takeRetainedValue()
}
```

> Important: If you provide a populated `vImage_CGImageFormat` with properties (such as bit-depth, color space, and channel ordering) that are different from those of the `CGImage` instance, vImage uses [any-to-any conversion](https://developer.apple.com/documentation/accelerate/functions-that-perform-any-to-any-conversion/) as part of the operation. If your app is latency-sensitive, provide either an empty `vImage_CGImageFormat` or a `vImage_CGImageFormat` with properties that match the source image to ensure that the operation avoids the conversion step.

---

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)