Question

I am trying to add a stroke around my hollow circle and its not appearing. Can someone help me please spot where I am going wrong and fix it please. Been stuck on this for ages and nothing seems to work!

This is how it currently looks when I run my code:

enter image description here

I want to add a stroke line around the outside of the hollow circle but my code isn't working and I don't know where I am going wrong. Would really appreciate some help.

Below is my code:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    // Fill full map rect with some color.
    CGRect rect = [self rectForMapRect:mapRect];
    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:255 alpha:0.4f].CGColor);
    CGContextFillRect(context, rect);
    CGContextRestoreGState(context);

    // Clip rounded hole.
    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillEllipseInRect(context, [self rectForMapRect:[self.overlay boundingMapRect]]);

    CGContextSetLineWidth(context, 1);

    CGContextStrokeEllipseInRect(context, [self rectForMapRect:[self.overlay boundingMapRect]]);
    CGContextRestoreGState(context);
    // Draw circle
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}

FIXED - WORKING CODE:

    // Fill full map rect with some color.
    CGRect rect = [self rectForMapRect:mapRect];
    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:255 alpha:0.4f].CGColor);

    CGContextFillRect(context, rect);
    CGContextRestoreGState(context);

    // Clip rounded hole.
    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 300);
    CGContextStrokeEllipseInRect(context, [self rectForMapRect:[self.overlay boundingMapRect]]);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillEllipseInRect(context, [self rectForMapRect:[self.overlay boundingMapRect]]);
    CGContextRestoreGState(context);
    // Draw circle
    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
Was it helpful?

Solution

Do this: CGContextSetLineWidth(context, 1); before the stroke is applied.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top