为什么Core Graphics会绘制这个大纲?

时间:2014-05-20 00:36:15

标签: ios core-graphics

出于某种原因,Core Graphics正在我的路径上绘制一个额外的轮廓。这是一些示例代码。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSaveGState(context);
float startRadius = 71.0;
float startAngle = 135, endAngle = 405;
endAngle = startAngle + (endAngle - startAngle)*drawValue;
int clockwise = 0;

if(isTargetFiller) {
    if(drawValue < 0.0)
        clockwise = 1;
    startAngle = 270;
    endAngle = 405;
    endAngle = startAngle + (endAngle - startAngle)*drawValue;
}

CGPoint center = CGPointMake(75, 75);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, startRadius, DEG_2_RAD(startAngle),
    DEG_2_RAD(endAngle), clockwise);
CGContextClosePath(context);

CGContextClip(context);

UIImage *bigImage = [UIImage imageNamed:@"CTracker_Full.png"];
CGPoint topLeftOffset = CGPointMake(
    (self.bounds.size.width - bigImage.size.width) / 2,
    (self.bounds.size.height - bigImage.size.height) / 2);

[bigImage drawAtPoint: topLeftOffset blendMode:kCGBlendModeNormal alpha:1.0];

CGContextSetBlendMode (context, kCGBlendModeMultiply);

float green = 1.0;
float red = 0.0;
if(value > 0.5) {
    green = 1.0 - (value - 0.5) / 0.5;
    red = (value - 0.5) / 0.5;
}

UIColor *tintColor = [UIColor colorWithRed:red green:green blue:0.0 alpha:1.0];

CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));
CGContextFillRect(context, rect);

CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
[bigImage drawAtPoint:topLeftOffset blendMode:kCGBlendModeDestinationIn
    alpha:1.0];

以下是目前正在绘制的内容:

enter image description here

如您所见,实际形状周围有一个轮廓,指向图像的中心。

1 个答案:

答案 0 :(得分:0)

在发布此内容后立即了解自己。希望这会帮助其他人。

将最后一点代码更改为:

UIColor *tintColor = [UIColor colorWithRed:red green:green blue:0.0 alpha:1.0];



CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));
CGContextFillRect(context, rect);

CGContextRestoreGState(context);
CGContextSaveGState(context);


CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
[bigImage drawAtPoint:topLeftOffset blendMode:kCGBlendModeDestinationIn alpha:1.0];