gpt4 book ai didi

ios - 使用 MKOverlayRenderer 绘制带孔的圆效果不佳

转载 作者:可可西里 更新时间:2023-11-01 05:54:30 25 4
gpt4 key购买 nike

我想在 mapview 上绘制带孔的圆(如 donut )。我的代码在这里。

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CG    ContextRef)context {

WPCircleOverlay * circleOverlay = self.overlay;

CGPoint centerPoint = [self pointForMapPoint:MKMapPointForCoordinate(circleOverlay.coordinate)];

CGFloat innerRadius = MKMapPointsPerMeterAtLatitude(circleOverlay.coordinate.latitude) * circleOverlay.innerRadius;
CGFloat outerRadius = MKMapPointsPerMeterAtLatitude(circleOverlay.coordinate.latitude) * circleOverlay.outerRadius;
CGMutablePathRef path = CGPathCreateMutable();

//CGPathMoveToPoint(path, ...);
CGPathAddArc(path, NULL, centerPoint.x, centerPoint.y, outerRadius, 0, 2 * M_PI, true);
CGPathCloseSubpath(path);

// Add the inner arc to the path (later used to substract the inner area)
CGPathAddArc(path, NULL, centerPoint.x, centerPoint.y, innerRadius, 0, 2 * M_PI, true);
CGPathCloseSubpath(path);

// Add the path to the context
CGContextAddPath(context, path);

CGContextSetFillColorWithColor(context, self.fillColor.CGColor);
CGContextEOFillPath(context);

CGPathRelease(path);

它在模拟器中运行良好,但在设备上则不然。

在设备上,外圈用颜色填充,内圈没有被剪裁。如何修改我的代码以在设备上正常运行?

最佳答案

我通过使用 CGContextAddEllipseInRect 方法而不是 CGContextAddArc 来修复它。

WPCircleOverlay * circleOverlay = self.overlay;

CGRect rectForMapRect = [self rectForMapRect:mapRect];
CGPoint centerPoint = [self pointForMapPoint:MKMapPointForCoordinate(circleOverlay.coordinate)];

CGFloat innerRadius = MKMapPointsPerMeterAtLatitude(circleOverlay.coordinate.latitude) * circleOverlay.innerRadius;
CGFloat outerRadius = MKMapPointsPerMeterAtLatitude(circleOverlay.coordinate.latitude) * circleOverlay.outerRadius;

CGRect innerRect = CGRectMake(centerPoint.x - innerRadius, centerPoint.y - innerRadius, innerRadius * 2.0, innerRadius * 2.0);
CGRect outerRect = CGRectMake(centerPoint.x - outerRadius, centerPoint.y - outerRadius, outerRadius * 2.0, outerRadius * 2.0);

if (CGRectIntersectsRect(rectForMapRect, outerRect)) {

CGContextAddRect(context, rectForMapRect);

CGContextSaveGState(context);
CGContextClip(context);

CGContextAddEllipseInRect(context, outerRect);
CGContextAddEllipseInRect(context, innerRect);

CGContextSaveGState(context);
CGContextEOClip(context);

UIColor * color = [self.fillColor copy];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, outerRect);

CGContextRestoreGState(context);
CGContextRestoreGState(context);

UIGraphicsPopContext();
}

关于ios - 使用 MKOverlayRenderer 绘制带孔的圆效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com