Signature.h file
#import <UIKit/UIKit.h>
@interface Signature : UIViewController
{
CGPoint firstTouch;
CGPoint lastTouch;
UIColor *pointColor;
CGRect *points;
int npoints;
CGPoint location;
// UIImageView *signatureImageView;
}
@property CGPoint firstTouch;
@property CGPoint lastTouch;
@property (nonatomic, retain) UIColor *pointColor;
@property CGRect *points;
@property int npoints;
@property (retain, nonatomic) IBOutletCollection(UIImageView) NSArray *drawSign;
@property CGPoint location;
@property (retain, nonatomic) IBOutletCollection(UIImageView) NSArray *signatureImageView;
- (IBAction)savePressed:(id)sender;
- (IBAction)clearPressed:(id)sender;
@end
Signature.m
#import "Signature.h"
@interface Signature ()
@end
@implementation Signature
@synthesize drawSign;
@synthesize signatureImageView;
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize pointColor;
@synthesize points;
- (id)initWithFrame:(CGRect)frame
{
return self;
}
- (id)initWithCoder:(NSCoder *)coder
{
if(self = [super initWithCoder:coder])
{
self.npoints = 0;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
NSLog(@"initwithnibname");
return self;
}
- (void)viewDidLoad
{
NSLog(@"viewdidload");
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
NSLog(@"view did unload");
[self setSignatureImageView:nil];
[self setDrawSign:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touch has began");
UITouch *touch = [touches anyObject];
self.location = [touch locationInView:self.view];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches moves");
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//[self.drawSign.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
NSLog(@"%f x is",location.x);
NSLog(@"%f y is",location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
// self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
location = currentLocation;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches ended");
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//[self.view.image drawInRect:CGRectMake(0, 0, self.frame.view.size.width, self.frame.view.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
// self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
location = currentLocation;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[signatureImageView release];
[super dealloc];
}
- (IBAction)savePressed:(id)sender {
NSLog(@"save pressed");
}
- (IBAction)clearPressed:(id)sender {
NSLog(@"cancel pressed");
}
@end
Please find the two files, related to my project. From One view I am coming to this view. Now I need to save whatever has been drawn on it by the user. I am able to fetch the co-ordinates but unable to draw them. I have tried several attempts but neither of them seems to be working.Please find them in the Signature.m file itself in commented way. Kindly point me my mistake and correct me.
I really dont understand your question but the reason this code is wrong is in the touches part.
If you want to draw while you touch then you have to use the drawInRect method from the view you want to draw in.
If you want to draw over an image to save or present it after the user releases it then you have to create an image context on the touchesBegan, then draw on it on the touchesMoved, and finally save the result in the touchesEnded.
You can extract the result at the touchesEnded with
What your code is doing right now is, every time you move your finger you create a new canvas, draw on it and destroy it. When you lift your finger you create one more canvas draw on it and destroy it as well.
But im pretty sure you want to move your finger and show the result directly in the screen. For that I really recommend you google for a tutorial but basically you have to: create an array of points in the touches begin, on the touches move you add points to this array and on the touches end you add the final point to the array. MEANWHILE on the view drawInRect your code has to continuously draw the points in this array.
EDIT:
ASSUMING you have a valid UIImage (blanc canvas already initialized) at the beginning in
self.drawSign.image
Try:
I am not sure if this will work, but even if it does it is a TERRIBLE WAY of doing it (performance wise).