<!--
{
  "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/makeDynamicPixelBufferAndCGImageFormat(cgImage:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate6vImageO11PixelBufferVAaC07DynamicC6FormatVRszrlE04makeecd10AndCGImageF002cgB0AEy_AGG05pixelD0_So01vb1_iF0V0jbF0tSo0I3Refa_tKFZ"
  },
  "title" : "makeDynamicPixelBufferAndCGImageFormat(cgImage:)"
}
-->

# makeDynamicPixelBufferAndCGImageFormat(cgImage:)

Returns a new dynamic pixel format pixel buffer and Core Graphics image format structure from a Core Graphics image.

```
static func makeDynamicPixelBufferAndCGImageFormat(cgImage: CGImage) throws -> (pixelBuffer: vImage.PixelBuffer<vImage.DynamicPixelFormat>, cgImageFormat: vImage_CGImageFormat)
```

## Parameters

`cgImage`

The source Core Graphics image.

## Return Value

A new pixel buffer of type [`vImage.DynamicPixelFormat`](/documentation/Accelerate/vImage/DynamicPixelFormat) and a [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) that describes the ordering and number of the color channels, the size and type of the data in the color channels, and whether or not the data is premultiplied by alpha.

## Discussion

Use this function to create an image format and pixel buffer from a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> instance. The pixel buffer that this function returns has a pixel format that’s unknown at compile time.

Note that you can call [`init(cgImage:cgImageFormat:pixelFormat:)`](/documentation/Accelerate/vImage/PixelBuffer/init(cgImage:cgImageFormat:pixelFormat:)) to create a statically typed pixel buffer from a <doc://com.apple.documentation/documentation/CoreGraphics/CGImage> instance. However, this function allows you to create your own any-to-any converter for more control over the conversion process to a statically typed buffer.

For example, the following code creates a dynamically typed pixel buffer and [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) from a source image. Use the returned [`vImage_CGImageFormat`](/documentation/Accelerate/vImage_CGImageFormat) as the source format on an any-to-any converter. For example:

```swift
let srcImage = imageLiteral(resourceName: " ... ").cgImage(
    forProposedRect: nil,
    context: nil,
    hints: nil)!

let source = try vImage.PixelBuffer.makeDynamicPixelBufferAndCGImageFormat(
    cgImage: srcImage)

let destFormat = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 8,
    colorSpace: CGColorSpaceCreateDeviceGray(),
    bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue))!

let destBuffer = vImage.PixelBuffer(size: source.pixelBuffer.size,
                                    pixelFormat: vImage.Planar8.self)

let converter = try vImageConverter.make(sourceFormat: source.cgImageFormat,
                                         destinationFormat: destFormat)

try converter.convert(from: source.pixelBuffer, to: destBuffer)
```

On return, `destBuffer` contains an 8-bit, grayscale representation of the original image.

---

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)