Here's a UIView subclass I use for drawing. I can't remember where I sourced it so if anyone recognises the code please give credit to the original programmer:
#import
#import
@interface SignatureView : UIView {
UIImage *myPic;
}
@property (strong, nonatomic) NSMutableArray *myDrawing;
@property (nonatomic) BOOL canDraw;
- (void) drawPic:(UIImage*)thisPic;
- (void) cancelDrawing;
- (UIImage*) collectSignature;
@end
#import "SignatureView.h"
@implementation SignatureView
@synthesize canDraw, myDrawing;
- (void)drawRect:(CGRect)rect
{
//round the corners
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
float newHeight;
float newWidth;
if (!myDrawing)
myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (myPic != NULL)
{
float ratio = myPic.size.height/460;
if (myPic.size.width/320 > ratio)
{
ratio = myPic.size.width/320;
}
newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;
[myPic drawInRect:CGRectMake(0,0, newWidth, newHeight)];
}
if ([myDrawing count] > 0)
{
CGContextSetLineWidth(ctx, 5);
for (int i = 0; i < [myDrawing count]; i++)
{
NSArray *thisArray = [myDrawing objectAtIndex:i];
if ([thisArray count] > 2)
{
float thisX = [[thisArray objectAtIndex:0] floatValue];
float thisY = [[thisArray objectAtIndex:1] floatValue];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, thisX, thisY);
for (int j = 2; j < [thisArray count] ; j+=2)
{
thisX = [[thisArray objectAtIndex:j] floatValue];
thisY = [[thisArray objectAtIndex:j+1] floatValue];
CGContextAddLineToPoint(ctx, thisX,thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
- (void)drawPic:(UIImage *)thisPic
{
myPic = thisPic;
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; //memory leak
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; //memory leak
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; //memory leak
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; //memory leak
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; //memory leak
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
[self setNeedsDisplay];
}
-(void)cancelDrawing
{
[myDrawing removeAllObjects];
[self setNeedsDisplay];
}
- (UIImage *)collectSignature
{
//take screenshot
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
@end