Skip to content

Instantly share code, notes, and snippets.

@watr
Created April 23, 2014 01:07
Show Gist options
  • Save watr/11199697 to your computer and use it in GitHub Desktop.
Save watr/11199697 to your computer and use it in GitHub Desktop.
Convenient category to create UIImage from UIBezierPath
#import <UIKit/UIKit.h>
@interface UIImage (BezierPath)
+ (UIImage *)imageWithBezierPathFill:(UIBezierPath *)bezierPath;
+ (UIImage *)imageWithBezierPathStroke:(UIBezierPath *)bezierPath;
@end
#import "UIImage+BezierPath.h"
@implementation UIImage (BezierPath)
+ (UIImage *)imageWithBezierPathFill:(UIBezierPath *)bezierPath
{
return [self imageWithBezierPath:bezierPath
fill:YES
stroke:NO
scale:[[UIScreen mainScreen] scale]];
}
+ (UIImage *)imageWithBezierPathStroke:(UIBezierPath *)bezierPath
{
return [self imageWithBezierPath:bezierPath
fill:NO
stroke:YES
scale:[[UIScreen mainScreen] scale]];
}
+ (UIImage *)imageWithBezierPath:(UIBezierPath *)bezierPath
fill:(BOOL)fill
stroke:(BOOL)stroke
scale:(CGFloat)scale
{
UIImage *image = nil;
if (bezierPath) {
UIGraphicsBeginImageContextWithOptions(bezierPath.bounds.size, NO, scale);
if (fill) {
[bezierPath fill];
}
if (stroke) {
[bezierPath stroke];
}
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment