我是 Objective-C 和 iOS 开发的新手。我有一个 View ,其中包含一些由核心绘图绘制的图形。现在我想用颜色填充这些形状,但我不知道形状的路径或上下文。 Objective-C 是否具有类似 flood-fill 或 put-pixel 的任何功能,以便仅通过描边颜色我就可以在我的 View 中填充任何形状。
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {1.0, 0.0, 0.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 50, 0);
CGContextAddLineToPoint(context, 0,320);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetRGBFillColor(context, 0, 255, 0, 1.0);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
这将创建一个圆形和一个具有公共(public)区域的正方形。当用户点击该区域时,我希望特定区域填充颜色。
最佳答案
所以你正在寻找 Objective C 中的 flood fill。
这是我对扫描线洪水填充算法的实现。
Github : UIImageScanlineFloodfill
如果你想学习基本的洪水填充:
Lode's Computer Graphics Tutorial Flood Fill
希望对您有所帮助。
关于iphone - 如何用颜色填充形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139853/