Last active
December 21, 2015 08:49
-
-
Save xNekOIx/6281078 to your computer and use it in GitHub Desktop.
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
// make image from color | |
#import <UIKit/UIKit.h> | |
@interface UIImage (NKOGraphics) | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color; | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color size:(CGSize)size; | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color size:(CGSize)size cornerRadius:(CGFloat)radius; | |
+ (UIImage*)nko_gradientImageWithTopColor:(UIColor*)topColor bottomColor:(UIColor*)bottomColor size:(CGSize)size radius:(CGFloat)radius; | |
@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
#import "UIImage+NKOGraphics.h" | |
@implementation UIImage (NKOGraphics) | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color | |
{ | |
return [self nko_imageWithColor:color size:CGSizeMake(1, 1)]; | |
} | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color size:(CGSize)size | |
{ | |
CGRect rect = { | |
.origin = CGPointZero, | |
.size = size | |
}; | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
[color setFill]; | |
CGContextFillRect(context, rect); | |
UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
+ (UIImage*)nko_imageWithColor:(UIColor*)color size:(CGSize)size cornerRadius:(CGFloat)radius | |
{ | |
CGRect rect = { | |
.origin = CGPointZero, | |
.size = size | |
}; | |
NSAssert(size.width > radius*2 && size.height > radius*2, @"Radius is bigger than expected"); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius]; | |
[path addClip]; | |
[color setFill]; | |
CGContextAddPath(context, [path CGPath]); | |
CGContextFillPath(context); | |
UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
+ (UIImage*)nko_gradientImageWithTopColor:(UIColor*)topColor bottomColor:(UIColor*)bottomColor size:(CGSize)size radius:(CGFloat)radius | |
{ | |
CGRect rect = { | |
.origin = CGPointZero, | |
.size = size | |
}; | |
NSAssert(size.width > radius*2 && size.height > radius*2, @"Radius is bigger than expected"); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGFloat locations[2] = {0.0, 1.0}; | |
NSArray* gradientColors = @[(id)[topColor CGColor], | |
(id)[bottomColor CGColor]]; | |
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, locations); | |
CGColorSpaceRelease(colorSpace); | |
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius]; | |
[path addClip]; | |
CGContextAddPath(context, [path CGPath]); | |
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, rect.size.height), kCGGradientDrawsAfterEndLocation); | |
CGGradientRelease(gradient); | |
UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment