Created
August 28, 2015 20:40
-
-
Save josefdlange/ed246bbf3829ba8c90f1 to your computer and use it in GitHub Desktop.
Resizing a UIImage, leveraging UIViewContentMode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (UIImage *)resizedWithNewSize:(CGSize)size contentMode:(UIViewContentMode)contentMode { | |
| CGFloat x = 0.0f; | |
| CGFloat y = 0.0f; | |
| CGFloat w = size.width; | |
| CGFloat h = size.height; | |
| BOOL scale = YES; | |
| switch (contentMode) { | |
| case UIViewContentModeScaleToFill: { | |
| break; | |
| } | |
| case UIViewContentModeScaleAspectFit: { | |
| CGFloat ratio = w / self.size.width; | |
| if(self.size.height * ratio > h) { | |
| ratio = h / self.size.height; | |
| } | |
| w = self.size.width * ratio; | |
| h = self.size.height * ratio; | |
| break; | |
| } | |
| case UIViewContentModeScaleAspectFill: { | |
| CGFloat ratio = w / self.size.width; | |
| if(self.size.height * ratio < h) { | |
| ratio = h / self.size.height; | |
| } | |
| w = self.size.width * ratio; | |
| h = self.size.height * ratio; | |
| break; | |
| } | |
| case UIViewContentModeRedraw: { | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeCenter: { | |
| CGFloat xInset = (self.size.width - w) / 2.0f; | |
| CGFloat yInset = (self.size.height - h) / 2.0f; | |
| x = -xInset; | |
| y = -yInset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeTop: { | |
| CGFloat xInset = (self.size.width - w) / 2.0f; | |
| x = -xInset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeBottom: { | |
| CGFloat xInset = (self.size.width - w) / 2.0f; | |
| CGFloat yOffset = (self.size.height - h); | |
| x = -xInset; | |
| y= yOffset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeLeft: { | |
| CGFloat yInset = (self.size.height - h) / 2.0f; | |
| y = -yInset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeRight: { | |
| CGFloat yInset = (self.size.height - h) / 2.0f; | |
| CGFloat xOffset = (self.size.width - w); | |
| y = -yInset; | |
| x = xOffset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeTopLeft: { | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeTopRight: { | |
| CGFloat xOffset = (self.size.width - w); | |
| x = xOffset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeBottomLeft: { | |
| CGFloat yOffset = (self.size.height - h); | |
| y = yOffset; | |
| scale = NO; | |
| break; | |
| } | |
| case UIViewContentModeBottomRight: { | |
| CGFloat xOffset = (self.size.width - w); | |
| CGFloat yOffset = (self.size.height - h); | |
| x = xOffset; | |
| y = yOffset; | |
| scale = NO; | |
| break; | |
| } | |
| default: { | |
| break; | |
| } | |
| } | |
| UIGraphicsBeginImageContext(CGSizeMake(w, h)); | |
| CGRect drawingRect = scale ? CGRectMake(x, y, w, h) : CGRectMake(x, y, self.size.width, self.size.height); | |
| [self drawInRect:drawingRect]; | |
| UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| return newImage; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment