0
votes

For some reason Core Graphics is drawing an extra outline around my path. Here is some sample code.

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];

Here is what it is currently drawing:

enter image description here

As you can see, there is an outline around the actual shape that leads to the center of the image.

1
I mean, you're clipping to a wedge shape... - nielsbot
Maybe you can clip with an inner circle as well? - nielsbot
Yes, but the last call to drawAtPoint (with the kCGBlendModeDestinationIn blend mode) is supposed to hide everything on the inside due to the fact that the entire center of the image has an alpha of zero. - Dmacpro
nielsbot - I could do that, but I want to figure out the underlying issue with this. - Dmacpro

1 Answers

0
votes

Figured it out myself right after posting this. Hopefully this will help someone else.

Changed the last little bit of code to:

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];