<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/vImage/PixelBuffer/withUnsafeBufferPointer(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate6vImageO11PixelBufferVA2A06StaticC6FormatRzrlE010withUnsafeD7Pointeryqd__qd__SRy13ComponentTypeQzGKXEKlF"
  },
  "title" : "withUnsafeBufferPointer(_:)"
}
-->

# withUnsafeBufferPointer(_:)

Calls the given closure with a pointer to the buffer’s contiguous storage.

```
func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Format.ComponentType>) throws -> R) rethrows -> R
```

## Parameters

`body`

A closure with an <doc://com.apple.documentation/documentation/Swift/UnsafeBufferPointer> parameter that points to the contiguous storage for the pixel buffer.

## Return Value

The return value, if any, of the body closure parameter.

## Discussion

You can use this function to simplify interoperation with other libraries and frameworks. For example, the following code uses Accelerate’s <doc://com.apple.documentation/documentation/Accelerate/vforce-library> library to raise each pixel value to the power of `1 / 2.2`:

```swift
let srcBuffer = vImage.PixelBuffer(pixelValues: [0.1, 0.2, 0.3, 0.4],
                                   size: vImage.Size(width: 4, height: 1),
                                   pixelFormat: vImage.PlanarF.self)

let destBuffer =  vImage.PixelBuffer(size: srcBuffer.size,
                                     pixelFormat: vImage.PlanarF.self)

var exponent = Float(1 / 2.2)
var count = Int32(4)

srcBuffer.withUnsafeBufferPointer { src in
    destBuffer.withUnsafeMutableBufferPointer { dest in
        
        vvpowsf(dest.baseAddress!,
                &exponent,
                src.baseAddress!,
                &count)
    }
}
```

> Note:
> The contiguous storage may include space outside of the buffer’s width that doesn’t contain image information. The storage contains `rowStride * channelCount * height` elements.

---

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)