I am a new member of stack overflow, but I have been using the answers found in these forums for years now. I have a question that might be better answered with more thorough studying on the topic on my part, but I am doing a collaboration for one of my classes in school and have been commissioned to make some changes to someone else's code... and frankly I am running low on time so I was hoping to get some help here.
I have researched some of the other questions and answers found here but none of them seem to address my question directly.
I have an application that is supposed to capture a persons signature. It is currently working great for the iphone 4, the problem comes when I attempt to change hardware. if I select a 3.5 inch screen, or any version of the ipad the touches that I make with my signature are accurate at the top of the screen but gradually get less and less accurate as I move down (it is as if the image being portrayed is on a 4 inch screen, while the touches are happening on a 3.5 inch screen)
Here is my code for the ViewController:
@implementation SignatureViewController
@synthesize clearButton;
@synthesize tempDrawImage;
@synthesize tempSaveImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 5.0;
opacity = 1.0;
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
HousingAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
appDelegate.tempSig = self.tempDrawImage.image;
}
- (IBAction)clearSig:(id)sender {
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
return;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)viewDidDisappear:(BOOL)animated {
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
}
@end
I would assume that the "self.view.frame.size.width" and "self.view.frame.size.height" in the touchesMoved method would pull the information about the screen size of the hardware and populate this dynamically but apparently this is not so.
Any help would be greatly appreciated.
Also, currently the signature is being saved transposed on top of the bg-signature-portriat.png background image (a simple line with an x on it). I would like to get the signature alone but cannot fathom how to do this (perhaps this would require layering of UIImage? and something that cannot be easily explained without access to the storyboard?).
Thank you so much in advance, and I promise to have better questions (and hopefully a few answers) moving forward.
Try This it might help you and easy to change the code
- (void)showPopOverscreenforSignatureScreen //:(UIButton *)sender call this method in button action
{
mouseMoved = 0;
[SignatureContentView setHidden:NO];
SignatureContentView.frame=CGRectMake(kLabelWidth+20, 150, 500, 200);
signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
[signaturedImageView setHidden:YES];
}
- (void)allocPopOverscreenforSignatureScreen
{
SignatureContentView =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 500, 200)];
SignatureContentView.layer.cornerRadius=5.0f;
SignatureContentView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
SignatureContentView.backgroundColor=[UIColor grayColor];
[self.view addSubview:SignatureContentView];
signatureImageView = [[UIImageView alloc] initWithImage:nil];
signatureImageView.layer.cornerRadius=5.0f;
signatureImageView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
signatureImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper_.png"]];
signatureImageView.frame = CGRectMake(0, 0, 500, 148);
signatureImageView.userInteractionEnabled=YES;
[SignatureContentView addSubview:signatureImageView];
mouseMoved = 0;
UIButton *cancelButton =[UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame=CGRectMake(50, 150, 100, 40);
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
cancelButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[cancelButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:cancelButton];
UIButton *clearButton =[UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame=CGRectMake(200, 150, 100, 40);
[clearButton setTitle:@"Clear" forState:UIControlStateNormal];
clearButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[clearButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[clearButton addTarget:self action:@selector(clearsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:clearButton];
UIButton *useButton =[UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame=CGRectMake(350, 150, 100, 40);
useButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[useButton setTitle:@"Use" forState:UIControlStateNormal];
[useButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[useButton addTarget:self action:@selector(getSignaturefromsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:useButton];
[SignatureContentView setHidden:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = NO;
//The below statements will help to get the exact point user touches with out this it will take ZERO yaxis
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:signatureImageView];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:signatureImageView];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(signatureImageView.frame.size);
[signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 20) {
mouseMoved = 0;
}
}
- (void)clearsignatureImageView
{
signatureImageView.image = nil;
}
- (void)cancelsignatureImageView
{
[SignatureContentView setHidden:YES];
[signaturedImageView setHidden:NO];
}
- (void)getSignaturefromsignatureImageView
{
signaturedImageView.image=nil;
if (signatureImageView.image !=nil) {
AppDelegate *delegate =[AppDelegate sharedInstanceofAppdelegate];
UIGraphicsBeginImageContext(signatureImageView.frame.size);
[signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
// UIImageWriteToSavedPhotosAlbum(signatureImageView.image, nil, nil, nil);// black
UIGraphicsEndImageContext();
[SignatureContentView setHidden:YES];
[signaturedImageView setHidden:NO];
NSArray *docArr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath =[NSString stringWithFormat:@"%@/%@",[docArr objectAtIndex:0],[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
filePath =[[docArr objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
NSData* pictureData = UIImagePNGRepresentation(signatureImageView.image);
BOOL success= [pictureData writeToFile:filePath atomically:YES ];
if (success) {
delegate.userSignatureImagePathString=filePath;
}
delegate.userSignatureImage=signatureImageView.image;
signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
signaturedImageView.image=delegate.userSignatureImage;
}
else
{
// please sign
}
}
-(void)viewDidLoad
{
[self allocPopOverscreenforSignatureScreen];
}
Use the UIGestureRecognizerDelegate
delegate and declare these objects globally:
UIImageView *signatureImageView, *signaturedImageView;
BOOL mouseSwiped;
int mouseMoved;
UIView * SignatureContentView;