简体   繁体   中英

How can I smooth the line between 2 points in iOS?

I already know how to draw a line between 2 points, but the line seems not so smooth. What can I do to make it smoother? Thanks you.

- (void)drawLineFrom:(CGPoint)start To:(CGPoint)end {
// begin image context
UIGraphicsBeginImageContext(self.imageLineView.frame.size);

// define image rect for drawing
[self.imageLineView.image drawInRect:CGRectMake(0, 0, imageLineView.frame.size.width, imageLineView.frame.size.height)];

// set line properties
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, .0f, .0f, 1.0);

// move context to start point
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), start.x, start.y);

// define path to end point
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), end.x, end.y);

// stroke path
CGContextStrokePath(UIGraphicsGetCurrentContext());

// flush context to be sure all drawing operations were processed
CGContextFlush(UIGraphicsGetCurrentContext());

// get UIImage from context and pass it to our image view
imageLineView.image = UIGraphicsGetImageFromCurrentImageContext();

// end image context
UIGraphicsEndImageContext();
}

在此处输入图片说明

You can draw smooth line using Bezier Path.

You can have some more information here. Bézier Paths

Try to enable Anti-Aliasing. you'll need to set the UIViewEdgeAntialiasing key to YES in your Info.plist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM