iphone - 如何用颜色填充形状

标签 iphone ios core-graphics flood-fill

我是 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/

相关文章:

iPhone彩色图像分析

iphone - 点击时隐藏导航栏

iphone - 找到结束/结束坐标你一个 UISwipeGestureRecognizer

ios - 在 iphone 中使用 XMPP 创建自定义节

iOS 正在播放类似于音乐/Spotify 应用程序的 View

objective-c - 如何复制 UIRefreshControl 的 "pulled chewing gum"外观?

ios - CIDissolveTransition 问题。 kCIAttributeTypeTime 仅适用于 0.0

ios - 如何在 CGContext 中旋转 NSString drawinrect

iOS 7 如何自定义按钮 UIActionSheet

iphone - 减小图像大小(以 KB 为单位)