Skip to content

Instantly share code, notes, and snippets.

@epologee
Last active December 16, 2015 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epologee/5413892 to your computer and use it in GitHub Desktop.
Save epologee/5413892 to your computer and use it in GitHub Desktop.
[UIImage tttImageWithSize:CGSizeMake(44, 44)
drawing:^(CGContextRef ctx, CGSize size) {
UIColor *blue = [UIColor blueColor];
CGContextSetFillColorWithColor(ctx, blue.CGColor);
CGFloat half = size.height / 2;
rect = (CGRect){{0, half}, {size.width, half}};
CGContextFillRect(ctx, rect);
}];
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void (^TTTDrawingBlock)(CGContextRef ctx, CGSize size);
@interface UIImage (TTTDrawing)
+ (UIImage *)tttImageWithSize:(CGSize)size drawing:(TTTDrawingBlock)drawing;
@end
#import "UIImage+TTTDrawing.h"
@implementation UIImage (TTTDrawing)
+ (UIImage *)tttImageWithSize:(CGSize)size drawing:(TTTDrawingBlock)drawing
{
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
drawing(context, size);
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