Skip to content

Instantly share code, notes, and snippets.

@wess
Created February 28, 2012 22:50
Show Gist options
  • Save wess/1935821 to your computer and use it in GitHub Desktop.
Save wess/1935821 to your computer and use it in GitHub Desktop.
Scale UIImage
- (UIImage *)withNewSize:(CGSize)newSize
{
CGRect newRect = CGRectIntegral(CGRectMake(0.0f, 0.0f, newSize.width, newSize.height));
CGImageRef imageRef = self.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, newRect, imageRef);
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment