Skip to content

Instantly share code, notes, and snippets.

@dcai
Created October 11, 2011 14:22
Show Gist options
  • Save dcai/1278207 to your computer and use it in GitHub Desktop.
Save dcai/1278207 to your computer and use it in GitHub Desktop.
shrinkImage
static UIImage *shrinkImage(UIImage *original, CGSize size) {
CGFloat scale = [UIScreen mainScreen].scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context,
CGRectMake(0, 0, size.width * scale, size.height * scale),
original.CGImage);
CGImageRef shrunken = CGBitmapContextCreateImage(context);
UIImage *final = [UIImage imageWithCGImage:shrunken];
CGContextRelease(context);
CGImageRelease(shrunken);
return final;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment