Draw a line by touch On UIImageView in iPhone

Its much useful for drawing something on screen by touch.

1) Declare 4 objects in .h file

CGPoint lastPoint;

BOOL mouseSwiped;

int mouseMoved;

UIImageView *signImage;
2) First create a UIImageView object ie. a blank image in viewDidLoad

signImage = [[UIImageView alloc ] initWithFrame:CGRectMake(15, 60, 290, 320)];

signImage.image = nil;

signImage.tag = 1000;

signImage.layer.borderWidth = 3.0;

signImage.layer.borderColor = [[UIColor lightGrayColor] CGColor];

[self.view addSubview:signImage];

 

2) Then add 3 methods TouchesBegin, TouchesMoved & TouchesEnd

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

mouseSwiped = NO;

UITouch *touch = [touches anyObject];

CGPoint touchLocation = [touch locationInView:self.view];

 

if (CGRectContainsPoint(signImage.frame, touchLocation))

{

lastPoint = [touch locationInView:signImage];

}

 

NSLog(@”Last :%.2f – %.2f”,lastPoint.x, lastPoint.y);

}

 

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;

CGPoint currentPoint;

UITouch *touch = [touches anyObject];

CGPoint touchLocation = [touch locationInView:self.view];

 

if (CGRectContainsPoint(signImage.frame, touchLocation))

{

currentPoint = [touch locationInView:signImage];

UIGraphicsBeginImageContext(signImage.frame.size);

[first_signImage.image drawInRect:CGRectMake(0, 0, signImage.frame.size.width, signImage.frame.size.height)];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);

CGContextBeginPath(UIGraphicsGetCurrentContext());

CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

CGContextStrokePath(UIGraphicsGetCurrentContext());

signImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

 

//currentPoint.y -= 150;

 

lastPoint = currentPoint;

 

mouseMoved++;

 

if (mouseMoved == 10) {

mouseMoved = 0;

}

 

}

 

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

if(!mouseSwiped) {

 

CGPoint currentPoint;

UITouch *touch = [touches anyObject];

CGPoint touchLocation = [touch locationInView:self.view];

 

if (CGRectContainsPoint(signImage.frame, touchLocation))

{

currentPoint = [touch locationInView:signImage];

 

UIGraphicsBeginImageContext(signImage.frame.size);

[signImage.image drawInRect:CGRectMake(0, 0, signImage.frame.size.width, signImage.frame.size.height)];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);

CGContextBeginPath(UIGraphicsGetCurrentContext());

CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

CGContextStrokePath(UIGraphicsGetCurrentContext());

signImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

}

}

 

 


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box