<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vImage/PixelBuffer/init(cgImage:cgImageFormat:pixelFormat:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate6vImageO11PixelBufferVA2A24InitializableFromCGImageRzAA06StaticC6FormatRzrlE02cgB00jbI005pixelI0AEy_xGSo0G3Refa_So01vb1_gI0VzxmtKcfc"
  },
  "title" : "init(cgImage:cgImageFormat:pixelFormat:)"
}
-->

# init(cgImage:cgImageFormat:pixelFormat:)

Returns a new pixel buffer initialized from a Core Graphics image.

```
init(cgImage: CGImage, cgImageFormat: inout vImage_CGImageFormat, pixelFormat: Format.Type = Format.self) throws
```

## Parameters

`cgImage`

The source image.

`cgImageFormat`

The format of the image. Pass an empty [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) to specify that the function populates `cgImageFormat` with the properties of the image. Pass a populated [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) to specify that the function converts the image data to the format you specify.

`pixelFormat`

The pixel format of the initialized buffer.

## Discussion

For example, the following code creates a single-channel, 8-bit per pixel buffer from a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> of unknown bit depth.

When you pass a populated `cgImageFormat`, the [`init(cgImage:cgImageFormat:pixelFormat:)`](/documentation/Accelerate/vImage/PixelBuffer/init(cgImage:cgImageFormat:pixelFormat:)) initializer performs the conversion from the <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> instance’s format to the [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) that you specify.

Note the `bitsPerComponent`, `bitsPerPixel`, `colorSpace`, and `bitmapInfo`.

```swift
 let cgImage = [ ... ]

 let pixelFormat = vImage.Planar8.self

 var imageFormat = vImage_CGImageFormat(
     bitsPerComponent: pixelFormat.bitsPerComponent, // 8
     bitsPerPixel: pixelFormat.bitsPerPixel,         // 8
     colorSpace: CGColorSpaceCreateDeviceGray(),
     bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue))!

 let buffer = vImage.PixelBuffer(
    cgImage: cgImage,
    cgImageFormat: &imageFormat,
    pixelFormat: pixelFormat)
```

The following code shows a similar workflow, but it creates a 32-bit per channel RGBA buffer:

```swift
 let cgImage = [ ... ]

 let pixelFormat = vImage.InterleavedFx4.self

 var imageFormat = vImage_CGImageFormat(
     bitsPerComponent: pixelFormat.bitsPerComponent, // 32
     bitsPerPixel: pixelFormat.bitsPerPixel,         // 32 * 4
     colorSpace: CGColorSpaceCreateDeviceRGB(),
     bitmapInfo: CGBitmapInfo(rawValue:
                                 kCGBitmapByteOrder32Host.rawValue |
                                 CGBitmapInfo.floatComponents.rawValue |
                                 CGImageAlphaInfo.noneSkipLast.rawValue))

 let buffer = vImage.PixelBuffer(
     cgImage: cgImage,
     cgImageFormat: &imageFormat,
     pixelFormat: pixelFormat)
```

If you pass an empty `cgImageFormat`, the [`init(cgImage:cgImageFormat:pixelFormat:)`](/documentation/Accelerate/vImage/PixelBuffer/init(cgImage:cgImageFormat:pixelFormat:)) initializer populates the [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) with the properties of the <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> instance. For example, the following code initializes a pixel buffer from a 8-bit-per-channel, 4-channel RGB image:

```swift
let cgImage = [ ... ]

var cgImageFormat = vImage_CGImageFormat()

let buffer = try vImage.PixelBuffer(
    cgImage: cgImage,
    cgImageFormat: &cgImageFormat,
    pixelFormat: vImage.Interleaved8x4.self)

// Prints "8  32  1 (`CGColorSpaceModel.rgb.rawValue`)".
print(cgImageFormat.bitsPerComponent,
      cgImageFormat.bitsPerPixel,
      cgImageFormat.colorSpace.takeRetainedValue().model.rawValue)
```

> Important:
> If you specify a populated ``doc://com.apple.accelerate/documentation/Accelerate/vImage_CGImageFormat``, its bits per component and bits per pixel must match those of the buffer’s `pixelFormat`. If you specify an empty ``doc://com.apple.accelerate/documentation/Accelerate/vImage_CGImageFormat``, the bits per component and bits per pixel of the <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> must match those of the buffer’s `pixelFormat`.

> 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)