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

# vImageBuffer_InitForCopyFromCVPixelBuffer(_:_:_:_:)

Initializes an array of vImage buffers in the order necessary to copy from a Core Video pixel buffer.

```
func vImageBuffer_InitForCopyFromCVPixelBuffer(_ buffers: UnsafeMutablePointer<vImage_Buffer>, _ converter: vImageConverter, _ pixelBuffer: CVPixelBuffer, _ flags: vImage_Flags) -> vImage_Error
```

## Parameters

`buffers`

An array of [`vImage_Buffer`](/documentation/Accelerate/vImage_Buffer) structures. The number of source buffers is the return value of [`vImageConverter_GetNumberOfSourceBuffers(_:)`](/documentation/Accelerate/vImageConverter_GetNumberOfSourceBuffers(_:)).

`converter`

A Core-Video-to-Core-Graphics [`vImageConverter`](/documentation/Accelerate/vImageConverter) instance.

`pixelBuffer`

A locked <doc://com.apple.documentation/documentation/CoreVideo/CVPixelBuffer> instance.

`flags`

The options to use when performing this operation.
  
  > Important:
  > Always pass the ``doc://com.apple.accelerate/documentation/Accelerate/kvImageNoAllocate`` flag to this function. The ``doc://com.apple.accelerate/documentation/Accelerate/kvImageNoAllocate`` flag instructs the function to initialize the buffers to read directly from a locked <doc://com.apple.documentation/documentation/CoreVideo/CVPixelBuffer> instance. All operations that use the buffers must be between calls to the <doc://com.apple.documentation/documentation/CoreVideo/CVPixelBufferLockBaseAddress(_:_:)> and <doc://com.apple.documentation/documentation/CoreVideo/CVPixelBufferUnlockBaseAddress(_:_:)> functions.

## 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 vImage library represents multiple plane Core Video pixel buffers as individual vImage buffers. Call [`vImageConverter_GetNumberOfSourceBuffers(_:)`](/documentation/Accelerate/vImageConverter_GetNumberOfSourceBuffers(_:)) to instantiate the correct number of source buffers. Use this function to initialize the vImage buffers that you pass as the sources to a Core-Video-to-Core-Graphics [`vImageConverter`](/documentation/Accelerate/vImageConverter) instance.

The following shows the code for creating the three source buffers required to represent a <doc://com.apple.documentation/documentation/CoreVideo/kCVPixelFormatType_420YpCbCr8Planar> pixel buffer. On return of [`vImageBuffer_InitForCopyFromCVPixelBuffer(_:_:_:_:)`](/documentation/Accelerate/vImageBuffer_InitForCopyFromCVPixelBuffer(_:_:_:_:)), the three vImage buffers contain the luminance, Cb, and Cr image data.

```swift
let colorSpace = CGColorSpaceCreateDeviceRGB()

guard
    let cvImageFormat = vImageCVImageFormat.make(
        format: .format420YpCbCr8Planar,
        matrix: kvImage_ARGBToYpCbCrMatrix_ITU_R_709_2.pointee,
        chromaSiting: .center,
        colorSpace: colorSpace,
        alphaIsOpaqueHint: false),
    
    var cgImageFormat = vImage_CGImageFormat(
        bitsPerComponent: 8,
        bitsPerPixel: 32,
        colorSpace: colorSpace,
        bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipFirst.rawValue),
        renderingIntent: .defaultIntent),
    
    let converterCVtoCG = try? vImageConverter.make(
        sourceFormat: cvImageFormat,
        destinationFormat: cgImageFormat) else {
    return
}

let sourceBufferCount = vImageConverter_GetNumberOfSourceBuffers(converterCVtoCG)
var sourceBuffers = (0 ..< sourceBufferCount).map { _ in
    return vImage_Buffer()
}

// `cvPixelBuffer` is the source Core Video pixel buffer.
CVPixelBufferLockBaseAddress(cvPixelBuffer,
                             CVPixelBufferLockFlags.readOnly)

// Initialize the source buffers.
vImageBuffer_InitForCopyFromCVPixelBuffer(
    &sourceBuffers,
    converterCVtoCG,
    cvPixelBuffer,
    vImage_Flags(kvImageNoAllocate ))

// The destination has an interleaved format with one or more channels, and 
// is encodable with a `vImage_CGImageFormat`.
assert(vImageConverter_GetNumberOfDestinationBuffers(converterCVtoCG) == 1)

// Initialize the destination buffer.
var destinationBuffer = vImage_Buffer()
vImageBuffer_Init(&destinationBuffer,
                  sourceBuffers[0].height,
                  sourceBuffers[0].width,
                  cgImageFormat.bitsPerPixel,
                  vImage_Flags(kvImageNoFlags))
defer {
    destinationBuffer.free()
}

// On return, `destinationBuffer` contains the RGB conversion of the image
// data in the Core Video pixel buffer.
vImageConvert_AnyToAny(converterCVtoCG,
                       sourceBuffers,
                       &destinationBuffer,
                       nil,
                       vImage_Flags(kvImageNoFlags))

CVPixelBufferUnlockBaseAddress(cvPixelBuffer,
                               CVPixelBufferLockFlags.readOnly)
```

---

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)