-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Scharf
committed
May 8, 2013
1 parent
4a2caaf
commit 3af0dd6
Showing
3 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file contains 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
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 |