Created
June 8, 2012 09:08
-
-
Save andoco/2894634 to your computer and use it in GitHub Desktop.
UIBezierPath category for creating a UIImage of the path
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 <UIKit/UIKit.h> | |
@interface UIBezierPath (Image) | |
/** Returns an image of the path drawn using a stroke */ | |
-(UIImage*) strokeImageWithColor:(UIColor*)color; | |
@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 "UIBezierPath+Image.h" | |
@implementation UIBezierPath (Image) | |
-(UIImage*) strokeImageWithColor:(UIColor*)color { | |
// adjust bounds to account for extra space needed for lineWidth | |
CGFloat width = self.bounds.size.width + self.lineWidth * 2; | |
CGFloat height = self.bounds.size.height + self.lineWidth * 2; | |
CGRect bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, width, height); | |
// create a view to draw the path in | |
UIView *view = [[[UIView alloc] initWithFrame:bounds] autorelease]; | |
// begin graphics context for drawing | |
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]); | |
// configure the view to render in the graphics context | |
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
// get reference to the graphics context | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// reverse the y-axis to match the opengl coordinate space | |
CGContextScaleCTM(context, 1, -1); | |
CGContextTranslateCTM(context, 0, -view.bounds.size.height); | |
// translate matrix so that path will be centered in bounds | |
CGContextTranslateCTM(context, -(bounds.origin.x - self.lineWidth), -(bounds.origin.y - self.lineWidth)); | |
// set color | |
[color set]; | |
// draw the stroke | |
[self stroke]; | |
// get an image of the graphics context | |
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); | |
// end the context | |
UIGraphicsEndImageContext(); | |
return viewImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment