---
title: "withPixelBufferIfAvailable(_:)"
framework: coreml
role: symbol
role_heading: Instance Method
path: "coreml/mlshapedarray/withpixelbufferifavailable(_:)"
---

# withPixelBufferIfAvailable(_:)

Reads the underlying pixel buffer.

## Declaration

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

## Parameters

- `body`: The closure to run with the pixel buffer.

## Return Value

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

Discussion Use this method to read the contents of the underlying pixel buffer. The pixel buffer is read only. Do not write to it. let array = MLShapedArray<Float16>(mutating: pixelBuffer, shape: [2, 3]) array.withPixelBuffer { backingPixelBuffer in      // read backingPixelBuffer here. }

## See Also

### Reading and writing the pixel buffer

- [withMutablePixelBufferIfAvailable(_:)](coreml/mlshapedarray/withmutablepixelbufferifavailable(_:).md)
