Last active
October 24, 2015 02:13
-
-
Save soltrinox/08e9e46ace816315531b to your computer and use it in GitHub Desktop.
UIImage add Padding with Dictionary of Floats
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 *) padImage:(UIImage *)image withPaddingDictionary:(NSDictionary *)padding { | |
CGFloat width = image.size.height; | |
CGFloat height = image.size.height; | |
NSLog(@"ORIG IMAGE SIZE = %f : %f", width, height); | |
NSNumber *imageTopPadding = [padding valueForKeyPath:@"top"]; | |
NSNumber *imageRightPadding = [padding valueForKeyPath:@"right"]; | |
NSNumber *imageBottomPadding = [padding valueForKeyPath:@"bottom"]; | |
NSNumber *imageLeftPadding = [padding valueForKeyPath:@"left"]; | |
width = width + imageRightPadding.floatValue + imageLeftPadding.floatValue; | |
height = height + imageTopPadding.floatValue + imageBottomPadding.floatValue; | |
NSLog(@"NEW SIZE = %f : %f", width, height); | |
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
UIGraphicsPushContext(context); | |
// Now we can draw anything we want into this new context. | |
CGPoint origin = CGPointMake([imageLeftPadding floatValue] / 2.0f, | |
[imageTopPadding floatValue] / 2.0f); | |
NSLog(@"OFFSET = %f : %f", origin.x, origin.y); | |
[image drawAtPoint:origin]; | |
// Clean up and get the new image. | |
UIGraphicsPopContext(); | |
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return newImage; | |
} | |
/* USAGE */ | |
UIImage *firstImage = [image imageWithSize:CGSizeMake(30.0, 30.0) ]; | |
firstImage = [self padImage:firstImage withPaddingDictionary:@{@"top" : @5.0F, @"right" : @5.0F , @"bottom" : @0.0F , @"left" : @0.0F } ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment