2016-07-07 50 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的信息,您可能會找到一種方法來抵消座標,使線條全部落在您想要的位置。

相關問題