Resize an image with vImage.
vImage provides fast and accurate high-level functions for image manipulation; for example, compositing, convolution, and histogram operations. It operates on common image formats through
v structures. vImage buffers describe the size of an image and the number of bytes in each row, and point to the image pixel data. Buffers are initialized from Core Graphics images, Core Video pixel buffers, or raw pixel data. The pixel data a buffer points to can be used to create a new Core Graphics image or can be copied into a Core Video pixel buffer.
In the simplest workflow, you convert an image to a vImage buffer, apply an operation to the buffer, and convert the buffer back to an image. In this example, the width and height of the result are one-third of the original:
Initialize an Image Format and vImage Buffers
To learn about initializing the buffers you'll need to perform a scaling operation, see Creating a Core Graphics Image Format and Creating and Populating Buffers from Core Graphics Images. In this example, you'll need the image format and buffers discussed in Creating and Populating Buffers from Core Graphics Images. However, you'll use the following code to initialize a destination buffer with a height and width that are a quarter of the source dimensions.
Apply the Scale Operation
If you're rescaling an image with premultiplied alpha (that is, with a
bitmap value with
CGImage), before you apply the scale operation, see Avoid Artifacts by Unpremultiplying.
Otherwise, with the source and destination buffers properly initialized, you're ready to perform the scaling operation. Because your format contains four 8-bit channels, you use the
v function. This function works equally well on all channel orderings; for example, RGBA or BGRA.
destination now contains the scaled version of
source. To learn how to display the scaled result to your user, see Creating a Core Graphics Image from a vImage Buffer.
After you've finished with the source and destination buffers, it's important that you free the memory allocated to them:
Avoid Artifacts by Unpremultiplying
If you're rescaling an image with premultiplied alpha, you may see artifacts in high-frequency regions of the image. To avoid this situation, unpremultiply the image data—that is, remove the premultiplied alpha value from the image data—before the scaling operation, and premultiply the scaled result.
This code shows the additional operations required, with error handling removed for brevity: