<!--
{
  "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_InitForCopyToCVPixelBuffer(_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@vImageBuffer_InitForCopyToCVPixelBuffer"
  },
  "title" : "vImageBuffer_InitForCopyToCVPixelBuffer(_:_:_:_:)"
}
-->

# vImageBuffer_InitForCopyToCVPixelBuffer(_:_:_:_:)

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

```
func vImageBuffer_InitForCopyToCVPixelBuffer(_ 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 destination buffers is the return value of [`vImageConverter_GetNumberOfDestinationBuffers(_:)`](/documentation/Accelerate/vImageConverter_GetNumberOfDestinationBuffers(_:)).

`converter`

A Core-Graphics-to-Core-Video [`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_GetNumberOfDestinationBuffers(_:)`](/documentation/Accelerate/vImageConverter_GetNumberOfDestinationBuffers(_:)) to instantiate the correct number of destination buffers. Use this function to initialize the vImage buffers that you pass as the destinations to a Core-Video-to-Core-Graphics [`vImageConverter`](/documentation/Accelerate/vImageConverter) instance.

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

```swift
let colorSpace = CGColorSpaceCreateDeviceRGB()

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

let destinationBufferCount = vImageConverter_GetNumberOfDestinationBuffers(converterCGtoCV)
var destinationBuffers = (0 ..< destinationBufferCount).map { _ in
    return vImage_Buffer()
}

// `cvPixelBuffer` is the destination Core Video pixel buffer.
CVPixelBufferLockBaseAddress(cvPixelBuffer,
                             CVPixelBufferLockFlags(rawValue: 0))

// Initialize the destination buffers.
vImageBuffer_InitForCopyToCVPixelBuffer(
    &destinationBuffers,
    converterCGtoCV,
    cvPixelBuffer,
    vImage_Flags(kvImageNoAllocate))

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

// On return, `destinationBuffers` contains the YpCbCr conversion of the RGB
// image data in the vImage source buffer.
// `sourceBuffer` is the source vImage buffer.
vImageConvert_AnyToAny(converterCGtoCV,
                       &sourceBuffer,
                       &destinationBuffers,
                       nil,
                       vImage_Flags(kvImageNoFlags))

CVPixelBufferUnlockBaseAddress(cvPixelBuffer,
                               CVPixelBufferLockFlags(rawValue: 0))
```

---

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)