如何平滑iOS中2点之间的界限?

时间:2013-06-03 08:33:54

标签: ios uiimageview drawing

我已经知道如何在2点之间绘制一条直线,但线条似乎不那么平滑。我该怎么做才能让它更顺畅?谢谢你。

- (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();
}

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以使用Bezier Path绘制平滑线。

您可以在此处获得更多信息。 Bézier Paths

答案 1 :(得分:0)

尝试启用抗锯齿功能。您需要在Info.plist中将UIViewEdgeAntialiasing键设置为YES。