Create a gist now

Instantly share code, notes, and snippets.

Embed
UIImage scale using vImage
-(UIImage*)mmg_imageScaledToFitSize:(CGSize)fitSize
{
// Create a vImage_Buffer from the CGImage
CGImageRef sourceRef = self.CGImage;
vImage_Buffer srcBuffer;
vImage_CGImageFormat format = {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
.bitmapInfo = (CGBitmapInfo)kCGImageAlphaFirst,
.version = 0,
.decode = NULL,
.renderingIntent = kCGRenderingIntentDefault,
};
vImage_Error ret = vImageBuffer_InitWithCGImage(&srcBuffer, &format, NULL, sourceRef, kvImageNoFlags);
if (ret != kvImageNoError)
{
free(srcBuffer.data);
return nil;
}
// Create dest buffer
const NSUInteger scale = (NSUInteger)[[UIScreen mainScreen] scale];
const NSUInteger dstWidth = (NSUInteger)fitSize.width * scale;
const NSUInteger dstHeight = (NSUInteger)fitSize.height * scale;
const NSUInteger bytesPerPixel = 4;
const NSUInteger dstBytesPerRow = bytesPerPixel * dstWidth;
uint8_t* dstData = (uint8_t*)calloc(dstHeight * dstWidth * bytesPerPixel, sizeof(uint8_t));
vImage_Buffer dstBuffer = {
.data = dstData,
.height = dstHeight,
.width = dstWidth,
.rowBytes = dstBytesPerRow
};
// Scale
ret = vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageHighQualityResampling);
free(srcBuffer.data);
if (ret != kvImageNoError)
{
free(dstData);
return nil;
}
// Create CGImage from vImage_Buffer
ret = kvImageNoError;
CGImageRef destRef = vImageCreateCGImageFromBuffer(&dstBuffer, &format, NULL, NULL, kvImageNoFlags, &ret);
free(dstData);
// Create UIImage
UIImage* destImage = [[UIImage alloc] initWithCGImage:destRef scale:0.0 orientation:self.imageOrientation];
// Free up the remaining memory
CGImageRelease(destRef);
return destImage;
}
@conroewuhao

This comment has been minimized.

Show comment
Hide comment
@conroewuhao

conroewuhao Jan 14, 2016

Learn a lot from this code,thanks~

Learn a lot from this code,thanks~

@victorstewart

This comment has been minimized.

Show comment
Hide comment
@victorstewart

victorstewart Oct 31, 2017

you can use the kvImageNoAllocate flag instead to do a no-copy CGImageRef creation in vImageCreateCGImageFromBuffer

victorstewart commented Oct 31, 2017

you can use the kvImageNoAllocate flag instead to do a no-copy CGImageRef creation in vImageCreateCGImageFromBuffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment