<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLShapedArray/withPixelBufferIfAvailable(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML13MLShapedArrayV26withPixelBufferIfAvailableyqd__Sgqd__So11CVBufferRefaKXEKlF"
  },
  "title" : "withPixelBufferIfAvailable(_:)"
}
-->

# withPixelBufferIfAvailable(_:)

Reads the underlying pixel buffer.

```
func withPixelBufferIfAvailable<R>(_ body: (CVPixelBuffer) throws -> R) rethrows -> R?
```

## Parameters

`body`

The closure to run with the pixel buffer.

## Return Value

The value returned from body, unless the shaped array doesn’t use a pixel buffer backing, in which
case the method ignores body and returns nil.

## Discussion

Use this method to read the contents of the underlying pixel buffer. The pixel buffer is read only. Do not write to it.

```swift
let array = MLShapedArray<Float16>(mutating: pixelBuffer, shape: [2, 3])
array.withPixelBuffer { backingPixelBuffer in
     // read backingPixelBuffer here.
}
```

---

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)