2016-04-19 42 views
0

在這段代碼中我使用CGContextRef到我UIView創建下一個圖片:添加圖像,這與UIBezier或CGContextRef繪製成的UIView子層

http://imgur.com/gPtBAEO

CGMutablePathRef arc = CGPathCreateMutable(); 
CGFloat lineWidth = 16.0; 
CGContextRef cont = UIGraphicsGetCurrentContext(); 
CGContextFlush(cont); 
CGContextSetStrokeColorWithColor(cont, [UIColor grayColor].CGColor); 
CGContextSetFillColorWithColor(cont, [UIColor clearColor].CGColor); 

for (int i = 0; i < 16; i++) { 
    CGPathAddArc(arc, NULL, cenPoint.x, cenPoint.y, halfWidthInc(-8.0f), DEG_TO_RAD(_deg1*i), DEG_TO_RAD(_deg1*(i+1)), NO); 
    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, lineWidth, kCGLineCapButt, kCGLineJoinMiter, 10); 
    CGContextAddPath(cont, strokedArc); 
} 

for (int i = 0; i < 8; i++) { 
    arc = CGPathCreateMutable(); 
    CGPathAddArc(arc, NULL, cenPoint.x, cenPoint.y, halfWidthInc(-24.0f), DEG_TO_RAD(_deg2*i), DEG_TO_RAD(_deg2*(i+1)), NO); 
    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, lineWidth, kCGLineCapButt, kCGLineJoinMiter, 10); 
    CGContextAddPath(cont, strokedArc); 
} 

for (int i = 0; i < 4; i++) { 
    arc = CGPathCreateMutable(); 
    CGPathAddArc(arc, NULL, cenPoint.x, cenPoint.y, halfWidthInc(-40.0f), DEG_TO_RAD(_deg3*i), DEG_TO_RAD(_deg3*(i+1)), NO); 
    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, lineWidth, kCGLineCapButt, kCGLineJoinMiter, 10); 
    CGContextAddPath(cont, strokedArc); 
} 

for (int i = 0; i < 2; i++) { 
    arc = CGPathCreateMutable(); 
    CGPathAddArc(arc, NULL, cenPoint.x, cenPoint.y, halfWidthInc(-56.0f), DEG_TO_RAD(_deg4*i), DEG_TO_RAD(_deg4*(i+1)), NO); 
    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, lineWidth, kCGLineCapButt, kCGLineJoinMiter, 10); 
    CGContextAddPath(cont, strokedArc); 
} 


CGContextDrawPath(cont, kCGPathFillStroke); 

但我想用CATransform3D改造英寸對於它,我必須在我的UIView的子圖層中繪製這個上下文(因爲我想在它上面繪製更多的子圖層)。 如何在單獨的UIView子圖中繪製此CGContextRef路徑?

回答

0

我旁邊建築修復:

CGPathRef *board = CGContextCopyPath(cont); 

_indi1 = [CAShapeLayer layer]; 
_indi1.frame = self.layer.bounds; 
_indi1.bounds = CGRectInset(pathCont, 0, 0); 
_indi1.geometryFlipped = YES; 
_indi1.path = board; 
_indi1.strokeColor = [[UIColor colorWithRed:125.0f/255.0f green:251.0f/255.0f blue:181.0f/255.0f alpha:1] CGColor]; 
_indi1.fillColor = nil; 
_indi1.lineWidth = 1.0f; 
_indi1.fillRule = kCAFillRuleEvenOdd; 
_indi1.lineJoin = kCALineJoinRound; 

[self.layer insertSublayer:_indi1 atIndex:0]; 

_indi1 - 與CAShapeLayer型超類的屬性。 這是有效的,但結果看起來沒有平滑(與原始的像素化)。 喜歡傾向:enter image description here

我該如何解決這個問題,讓圖片更流暢?

P.S .:這個問題彈出,當我嘗試將CGContext轉換爲CAShapeLayer。在第一個例子中,當我用CGContext製作它時,這個問題還沒有解決。 (你可以看第一個例子在頂端的帖子)