在iPhone中绘制空心圆

RVN*_*RVN 12 iphone geometry cgcontext

我需要绘制以下图像 在此输入图像描述

灰色部分是我想在另一个图像上绘制我需要使用CGContext方法使用的代码,我尝试使用CGContextAddArc但是失败了因为当我填充笔划时,中心凹陷也填充了灰色纹理.

任何帮助赞赏.

信息:我有完整的蓝色图像,我需要在蓝色图像上方添加半圆

谢谢

Ole*_*ann 25

在Core Graphics文档中查看填充路径.基本上,您所做的是在路径(外部和内部)中添加两个弧,然后使用Core Graphics填充规则.代码看起来像这样:

CGMutablePathRef path = CGPathCreateMutable();

// Add the outer arc to the path (as if you wanted to fill the entire circle)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the inner arc to the path (later used to substract the inner area)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

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

// Fill the path using the even-odd fill rule
CGContextEOFillPath(context);

CGPathRelease(path);
Run Code Online (Sandbox Code Playgroud)


RVN*_*RVN 10

进一步研究Ole Begemann在他的回答和修改中提到的内容,我能够达到要求.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 40);
CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);
Run Code Online (Sandbox Code Playgroud)

因此,我只用了一个弧线而不是2个弧线,并用更高的宽度抚摸它.