Skip to content

Instantly share code, notes, and snippets.

@ThinhPhan
Created June 18, 2015 08:07
Show Gist options
  • Save ThinhPhan/d52cd6102aae96e7692f to your computer and use it in GitHub Desktop.
Save ThinhPhan/d52cd6102aae96e7692f to your computer and use it in GitHub Desktop.
UIImage category for resizing
// UIImage+SimpleResize.h
//
// Created by Robert Ryan on 5/19/11.
#import <Foundation/Foundation.h>
/** Image resizing category.
*
* Modified by Robert Ryan on 5/19/11.
*
* Inspired by http://ofcodeandmen.poltras.com/2008/10/30/undocumented-uiimage-resizing/
* but adjusted to support AspectFill and AspectFit modes.
*/
@interface UIImage (SimpleResize)
/** Resize the image to be the required size, stretching it as needed.
*
* @param newSize The new size of the image.
* @param contentMode The `UIViewContentMode` to be applied when resizing image.
* Either `UIViewContentModeScaleToFill`, `UIViewContentModeScaleAspectFill`, or
* `UIViewContentModeScaleAspectFit`.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode;
/** Crop the image to be the required size.
*
* @param bounds The bounds to which the new image should be cropped.
*
* @return Cropped `UIImage`.
*/
- (UIImage *)imageByCroppingToBounds:(CGRect)bounds;
/** Resize the image to be the required size, stretching it as needed.
*
* @param newSize The new size of the image.
*
* @return Resized `UIImage` of resized image.
*/
- (UIImage*)imageByScalingToFillSize:(CGSize)newSize;
/** Resize the image to fill the rectange of the specified size, preserving the aspect ratio, trimming if needed.
*
* @param newSize The new size of the image.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize;
/** Resize the image to fit within the required size, preserving the aspect ratio, with no trimming taking place.
*
* @param newSize The new size of the image.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize;
@end
// UIImage+SimpleResize.m
//
// Created by Robert Ryan on 5/19/11.
#import "UIImage+SimpleResize.h"
@implementation UIImage (SimpleResize)
- (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode
{
if (contentMode == UIViewContentModeScaleToFill)
{
return [self imageByScalingToFillSize:newSize];
}
else if ((contentMode == UIViewContentModeScaleAspectFill) ||
(contentMode == UIViewContentModeScaleAspectFit))
{
CGFloat horizontalRatio = self.size.width / newSize.width;
CGFloat verticalRatio = self.size.height / newSize.height;
CGFloat ratio;
if (contentMode == UIViewContentModeScaleAspectFill)
ratio = MIN(horizontalRatio, verticalRatio);
else
ratio = MAX(horizontalRatio, verticalRatio);
CGSize sizeForAspectScale = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
UIImage *image = [self imageByScalingToFillSize:sizeForAspectScale];
// if we're doing aspect fill, then the image still needs to be cropped
if (contentMode == UIViewContentModeScaleAspectFill)
{
CGRect subRect = CGRectMake(floor((sizeForAspectScale.width - newSize.width) / 2.0),
floor((sizeForAspectScale.height - newSize.height) / 2.0),
newSize.width,
newSize.height);
image = [image imageByCroppingToBounds:subRect];
}
return image;
}
return nil;
}
- (UIImage *)imageByCroppingToBounds:(CGRect)bounds
{
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
- (UIImage*)imageByScalingToFillSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize
{
return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFill];
}
- (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize
{
return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFit];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment