in Education by
is there an easy way to not draw this points in my lines? I don't know why this points are there because i never release my finger from screen during drawing of a line. I got the code from a drawing example. // draw a line - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; currentPoint.y -= 0; // 20 only for 'kCGLineCapRound' UIGraphicsBeginImageContext(self.view.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(), brushSize); // for size CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha); //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; mouseMoved++; if (mouseMoved == 10) { mouseMoved = 0; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //Draw a dot if(!mouseSwiped) { UIGraphicsBeginImageContext(self.view.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(), brushSize); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } } This is the final version with unique alpha, color, brushSize for every line: - (void) updateDrawingBoard { UIGraphicsBeginImageContext(self.drawImage.bounds.size); for ( NSDictionary *dict in paths ) { UIBezierPath *p = (UIBezierPath*)[dict objectForKey:@"path"]; p.lineWidth = [[dict objectForKey:@"size"]floatValue]; [[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue] green:[[dict objectForKey:@"green"]floatValue] blue:[[dict objectForKey:@"blue"]floatValue] alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke]; [p stroke]; } [[UIColor colorWithRed:r green:g blue:b alpha:alpha] setStroke]; [path stroke]; self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage]; path = [[UIBezierPath bezierPath] retain]; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinBevel; path.lineWidth = brushSize; [path moveToPoint:touchPoint]; [self updateDrawingBoard]; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage]; [path addLineToPoint:touchPoint]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: path,@"path", [NSNumber numberWithFloat:r], @"red", [NSNumber numberWithFloat:g], @"green", [NSNumber numberWithFloat:b], @"blue", [NSNumber numberWithFloat:alpha], @"alpha", [NSNumber numberWithFloat:brushSize], @"size", nil]; [paths addObject:dict]; [path release]; path = nil; [self updateDrawingBoard]; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage]; [path addLineToPoint:touchPoint]; [self updateDrawingBoard]; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The problem is that you are gradually accumulating the line by drawing into an image. Where the old line and the new line overlap, you see "dots" from the overdraw. The solution is to accumulate your points in a path and draw the image afresh each time using that path. Since you will be drawing a single path, not multiple overlapping paths, you shouldn't see the dots. Outline of code: Some time before drawing begins, create a CGMutablePathRef. When you get a new point you want to add to your line, use CGPathAddLineToPoint. When it's time to draw the path, use CGContextAddPath to add the line to the context, then fill or stroke as desired. You could also use CGContextDrawPath. Alternatively, you can use UIBezierPath instead of CGMutablePathRef, in which case the steps are: Create a UIBezierPath. Use -addLineToPoint: to add lines to the path. Use -stroke, -fill, and similar to draw the path into the context. This is likely to be simpler if you are not accustomed to working with CoreGraphics directly. I would drop the intermediate image and move this code into a view class (LineDrawView or CanvasView or something) rather than leaving the code in a view controller. Instead of updating an image, the view can just draw itself directly to the screene. The view would have methods to clear the path, undo strokes, and create an image of the path. The view controller would then use these to clear the canvas, undo lines, and save the drawing. You could enrich this later with functionality to configure the line style.

Related questions

0 votes
    The Cookies Preferences box that always appears in the bottom right hand corner of the dataplatform.ibm.com ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Select * from OPENQUERY (PORTAL, ''SELECT st.last AS "Last Name", st.first AS "First Name", ct. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    This one has been bugging me for a while now. Is there a way I can stop Intellj IDEA from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    my tens digit is one more than my hundreds digit. my hundreds digit is an even number between 5 and 7. my ones ... triangle. Who am l? Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    How to delete unpublished Git commits and get rid of them permanently? A. git reset –hard B. git revert C. git reset –soft...
asked Dec 20, 2022 in Technology by JackTerrance
0 votes
    When I'm making a request to my backend through a mutation like that: mutation{ resetPasswordByToken(token:" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    How to get rid of Date Filter in Salesforce?...
asked Nov 12, 2020 in Technology by JackTerrance
0 votes
    State the appropriate concept for the given statement. The aspect of liberty is expressed by Berlin’s statement “I am my own master”. Please answer the above question....
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    8. I can't think of insulting my friend, 9. She loves her parents immensely, 10. I am not going to give up ... merry look in her eyes, Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    Heyy mates..Am an icse student can anyone tell me any importance for java please its urgent I have my exam ... please for icse java. Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    My body is radially symmetrical. Water vascular system is present in my body. I am referred as ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Though I am multicellular, there are no tissues in my body. What is the name of my phylum? ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Tell me who am I? What is my class/ phylum? 1. My body is divided into proboscis, collar ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
...