withUnsafeBufferPointer(_:)
Calls the given closure with a pointer to the buffer’s contiguous storage.
Declaration
func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Format.ComponentType>) throws -> R) rethrows -> RParameters
- body:
A closure with an 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 vForce library to raise each pixel value to the power of 1 / 2.2:
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)
}
}