2016-07-07 54 views
0

我使用CGContext创建一个漏斗形状,首先绘制一个三角形,然后是一条线。我在我的UIView子类的drawRect中实现了这一点,但是想知道是否有其他绘制方法,因为这些线条有些模糊。有没有比CGContext更好的选择?

override func drawRect(rect: CGRect) { 

     let context: CGContextRef = UIGraphicsGetCurrentContext()! 
     CGContextClearRect(context, rect); 

     let rectWidth: CGFloat = 15 

     // Line 
     CGContextSetRGBStrokeColor(context, 0, 0, 0, 1) 
     CGContextMoveToPoint(context, rectWidth/2, 5) 
     CGContextAddLineToPoint(context, rectWidth/2, 10) 
     CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) 
     CGContextSetLineCap(context, CGLineCap.Round) 
     CGContextSetLineWidth(context, 3.0) 
     CGContextStrokePath(context) 

     // Triangle 
     CGContextBeginPath(context); 
     CGContextMoveToPoint (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); 
     CGContextAddLineToPoint(context, CGRectGetMidX(rect), 8); 
     CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect)); 
     CGContextClosePath(context); 

     UIColor.blueColor().setFill() 
     CGContextFillPath(context); 

    } 

产生以下: enter image description here

但是,当我输入我设计的图像,图像更清晰(你可以告诉在iPhone上): enter image description here

那么,有没有一个更好的方式来实现这个输出? CGContext行有点模糊,所以我想知道是否有更好的选择,以及这些方法的优点和缺点。

+0

我看到两个图像同样模糊。 – Tricertops

回答

2

核心图形正在绘制事情模糊可能是因为你告诉它。 CG坐标系中每对整数坐标的位置恰好在网格线上 - 如果您在这些坐标之间绘制一个宽点划线,则会在网格线两侧跨过半个点。根据您绘制的位图上下文的比例,这可能会产生两列半阴影像素,而不是一列完全阴影像素 - 也就是模糊线条。

就你而言,你绘制的线条之间的一些点落在整像素边界上和一些在半像素边界上,所以在1x上显示某些线条会模糊,并在2x或3x显示其他线条将会。在Apple的绘图文档中阅读有关Points versus Pixels的信息,您可能会找到一种方法来抵消坐标,使线条全部落在您想要的位置。

相关问题