0

在这段代码中,我使用 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 上绘制更多子层)。如何在 UIView 的单独子层中绘制此 CGContextRef 路径?

4

1 回答 1

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 类型的超类的属性。它有效,但结果看起来没有平滑(使用原始像素化)。像这样:在此处输入图像描述

我怎样才能修复它并使图像更流畅?

PS:当我尝试将 CGContext 转换为 CAShapeLayer 时会弹出此问题。在第一个示例中,当我使用 CGContext 制作时,这个问题并没有出现。(您可以在顶部帖子的第一个示例中查看它)

于 2016-04-22T09:24:20.443 回答