2013-07-19 191 views
0

我是Iphone开发新手。边缘填充颜色

目前我正在着色应用程序。

我使用苹果的绘画应用程序作为参考创建我的应用程序。

我成功地创建应用程序,其中u可在给定的纹理图像的画面颜色

我做什么是我 创建延伸OpenGL的一个自定义的UIView,我发现它的触摸,并相应地绘制。 我还保留了包含大纲图像的背景UIImageView,所以感觉就像您在上面的图像那样。

一切正常 但我想填充颜色黑边

一样,如果一个图像有四方形它有黑边和方形的内部是空的,如果我触摸任何方应该填补这一方与选择的颜色(主要是我工作的不规则形状)

谁能告诉我怎样才能填补这一方

洪水填充算法中看起来慢,因为我有这将需要时间来填写一些大的图像中的颜色颜色

所以有没有简单的方法,通过它我可以填充颜色

一个示例代码将b非常有帮助的,因为我是新来的iPhone开发

回答

1

我implemnted这种特征在我最近的项目。不同之处在于:我只在边框填充颜色。

检查我的代码在这里,它可能会帮助你

// apply color to only border & return an image 
+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color 
{ 
    // load the image 
    UIImage *img = [UIImage imageNamed:name]; 

    // begin a new image context, to draw our colored image onto 
    UIGraphicsBeginImageContext(img.size); 

    // get a reference to that context we created 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // set the fill color 
    [color setFill]; 

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords 
    CGContextTranslateCTM(context, 0, img.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    // set the blend mode to color burn, and the original image 
    CGContextSetBlendMode(context, kCGBlendModeColorBurn); 
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); 
    CGContextDrawImage(context, rect, img.CGImage); 

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle 
    CGContextClipToMask(context, rect, img.CGImage); 
    CGContextAddRect(context, rect); 
    CGContextDrawPath(context,kCGPathFill); 

    // generate a new UIImage from the graphics context we drew onto 
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //return the color-burned image 
    return coloredImg; 
} 

欣赏节目!

+0

在我给我所有的四方形单个图像 里面,所以我需要检测每平方米的如(还记得我说过我工作的不规则形状) 那么,如何检测这些部分 我是否需要使用CGpath绘制这些形状? – chavo218

+0

是的,这也会检测不规则形状的边界。这绝对不是为你填充颜色内部的形状,我重复这是为着色只有边框/框架。但你可以通过这个想法来实现你的愿望。只需要经过一次 –

+0

我正在使用opengl。我没有使用Core Graphics – chavo218