Skip to content

Commit

Permalink
added image manipulation class
Browse files Browse the repository at this point in the history
  • Loading branch information
David Scharf committed May 8, 2013
1 parent 4a2caaf commit 3af0dd6
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Bunch-o-Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
#import "UIColor+BOTFactories.h"

//UIFont Categories
#import "UIFont+BOTHelpers.h"
#import "UIFont+BOTHelpers.h"

//UIImage Categories
#import "UIImage+BOTManipulation.h"
17 changes: 17 additions & 0 deletions src/UIImage+Manipulation/UIImage+BOTManipulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIImage+BOTManipulation.h
//

#import <UIKit/UIKit.h>

@interface UIImage (BOTManipulation)

//scale image to a new size
- (UIImage *) scaleToSize:(CGSize) size;
- (UIImage *) scaleToSize:(CGSize) size retainAspect:(BOOL) retainAspect fit:(BOOL) fit;
- (UIImage *) scaleByValue:(float) scaleValue;

//inset image
- (UIImage *) inset:(UIEdgeInsets) insets;

@end
112 changes: 112 additions & 0 deletions src/UIImage+Manipulation/UIImage+BOTManipulation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// UIImage+BOTManipulation.m
//

#import "UIImage+BOTManipulation.h"

@implementation UIImage (BOTManipulation)

#pragma mark - scaling

- (UIImage *) scaleToSize:(CGSize) size retainAspect:(BOOL) retainAspect fit:(BOOL) fit
{
//prepare vars
CGRect frame = CGRectZero;
CGSize targetSize = size;
frame.size = targetSize;

//make sure ratio is preserved
if ( retainAspect ) {
float imageRatio = self.size.width / self.size.height;
float targetRatio = targetSize.width / targetSize.height;

//determine if we need to scale the height or the width to get to
//the right ratio
BOOL scaleHeight = imageRatio > targetRatio;

//if we want to fill instead of fit, reverse this
if ( !fit ) scaleHeight = !scaleHeight;

if ( scaleHeight ) {
//image breiter als target
targetSize.height = targetSize.width / imageRatio;
} else {
//image höher als target
targetSize.width = targetSize.height * imageRatio;
}

frame.size = targetSize;

// if fill
if ( !fit ) {
//move frame
frame.origin.x -= (targetSize.width - size.width) / 2.;
frame.origin.y -= (targetSize.height - size.height) / 2. ;

//target size is the original size requested
targetSize = size;
}
}

//begin context
UIGraphicsBeginImageContextWithOptions(targetSize, YES, self.scale);

//setup context, need to transform coordinates to screen space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, targetSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, frame, self.CGImage);
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return scaledImage;
}

- (UIImage *) scaleToSize:(CGSize) size
{
return [self scaleToSize:size retainAspect:NO fit:NO];
}

-(UIImage *)scaleByValue:(float)scaleValue
{
//we just need to scale the size of the new image
CGSize size = self.size;
size.width *= scaleValue;
size.height *= scaleValue;
return [self scaleToSize:size];
}

#pragma mark - insetting

-(UIImage *)inset:(UIEdgeInsets)insets
{
//prepare vars
CGRect frame = CGRectZero; frame.size = self.size;

//offset frame
frame.origin.y -= insets.top;
frame.origin.x -= insets.left;

//size of new image
CGSize newImageSize = self.size;
newImageSize.width -= (insets.left + insets.right);
newImageSize.height -= (insets.top + insets.bottom);

//begin context
UIGraphicsBeginImageContextWithOptions(newImageSize, YES, self.scale);

//setup context, need to transform coordinates to screen space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, newImageSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, frame, self.CGImage);

UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return scaledImage;
}

@end

0 comments on commit 3af0dd6

Please sign in to comment.