<!--
{
  "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/withMutablePixelBufferIfAvailable(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML13MLShapedArrayV33withMutablePixelBufferIfAvailableyqd__Sgqd__So11CVBufferRefaKXEKlF"
  },
  "title" : "withMutablePixelBufferIfAvailable(_:)"
}
-->

# withMutablePixelBufferIfAvailable(_:)

Writes to the underlying pixel buffer.

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

## Parameters

`body`

The closure to run with the pixel buffer.

## Discussion

Use this method to writes the contents of the underlying pixel buffer.

```swift
let array = MLShapedArray<Float16>(mutating: pixelBuffer, shape: [2, 3])
array.withMutablePixelBuffer { backingPixelBuffer in
     // write 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)