2

I am new to iPhone development,

I am making an application in which i am trying to make a line that is straight so that it doesn't has any irregular curves it must be smooth

I have used core graphics but it doesn't seem to be working.

Screen shot I Just want to make these lines drawn in red color with UITouch to be straight.

Should I go for openGl?

If yes, how can i implement.

  drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = viewField.frame;
[self.view addSubview:drawImage];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = NO;
UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
}

lastPoint = [touch locationInView:viewField];
//lastPoint.y = 20;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];    
CGPoint currentPoint = [touch locationInView:viewField];
//currentPoint.y -= 20; // only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(viewField.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;    
   }

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

  UITouch *touch = [touches anyObject];

  if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
  }
  if(!mouseSwiped) {
    //if color == green
    UIGraphicsBeginImageContext(viewField.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,   drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);   //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   }
   }

Thanks in Advance !

Deepranshu
  • 69
  • 1
  • 8
  • 2
    Good: "I'm a beginner so I'm gonna try things in my skill range, and improve while doing them, so that later I can do harder things". Bad: "I'm a beginner so I'm gonna try hard things, and instead of doing them myself I'm gonna ask around for the code to do it, that way I won't learn anything". – EmilioPelaez Jun 15 '12 at 03:49
  • What does the code you've written so far look like? Can you post it here? What part are you having trouble with? – user1118321 Jun 15 '12 at 04:35
  • 2
    If you want to draw a straight line, don't store every single point that has been touched. Store the start point and wherever the user's current touch is, and just draw a line from one to the other. It looks like you've copied a freehand drawing code sample fon somewhere which isn't what you want. – jrturton Jun 15 '12 at 06:56
  • Also, the question as it stands now is what you should have originally posted yesterday - it has the code you ate using, and a clear description of the problem - well done :) – jrturton Jun 15 '12 at 06:58
  • thanks @jrturton but can you please clear out what you are saying and show the error in the code by which i can get the desired result or should i change it completely. – Deepranshu Jun 15 '12 at 07:20
  • Do you understand what your current code is doing? – jrturton Jun 15 '12 at 07:24
  • Yes i know it is just adding points on the the view as the touch is moved. But can you suggest any thing else that is more suitable for the app i am trying to make. – Deepranshu Jun 15 '12 at 07:27
  • See here http://stackoverflow.com/questions/11050037/how-to-draw-automatic-line-between-two-tap-points-in-a-view-in-iphone – jrturton Jun 15 '12 at 16:22
  • Thanks But that question is posted by me only. – Deepranshu Jun 18 '12 at 03:44
  • http://iphoneapp-dev.blogspot.com please see it.and downlaod drawimage project.in this project you draw image an image is saved in photo library..you can save any where you want. –  Jun 19 '12 at 11:30
  • @MohitGupta thanks for your support but i am using the same code but the lines are not coming out to be straight. You can also see the snapshot attached to the questions. – Deepranshu Jun 19 '12 at 11:49
  • so exactly what do you want to do.please explain me properly.then i will help you. –  Jun 19 '12 at 11:51
  • So firstly you have to find out point then you can meet both points.then line will be straight –  Jun 19 '12 at 11:53
  • @MohitGupta i have made the points already but in touch and draw the lines are not straight. I just want to join the two points irrespective to the path taken in the drawing. – Deepranshu Jun 19 '12 at 11:57
  • http://pastebin.com/SMq0WvQh see it i have paste code on that link.. –  Jun 19 '12 at 12:08
  • @MohitGupta Can you please provide me the complete code. – Deepranshu Jun 20 '12 at 04:15
  • it's not possible because i don't know what you want do exactly.So firstly you have to tell me your requirement by step by step.by the way in stackoverflow nobody provide complete code.we can give you just an idea.. –  Jun 20 '12 at 04:24
  • @Deepranshu did you got the solution for your issue? actually i am facing the same issue http://stackoverflow.com/questions/24423537/ios-draw-line-with-both-arrow-with-start-end-point-while-finger-touch-end-and-ro – Nikunj Jadav Jul 26 '14 at 07:51

1 Answers1

0

Judging by the image you posted, I suggest you change your approach. Start drawing by double clicking. Then tap (= touchesStarted and touchesEnded, not touchesMoved) on every single edge point in your polygon. Draw a straight line between those points as you go along. End the polygon by Double Clicking. You could decide to close you path automatically if the end point is close enough to the start point.

Beav
  • 495
  • 2
  • 5
  • 14