Skip to content

Instantly share code, notes, and snippets.

@xNekOIx
Last active December 21, 2015 08:49
Show Gist options
  • Save xNekOIx/6281078 to your computer and use it in GitHub Desktop.
Save xNekOIx/6281078 to your computer and use it in GitHub Desktop.
// 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
#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