2013-01-07 113 views
0

嗨朋友, 我正在开发彩色填充游戏。我知道如何着色全图像,但我想在透明区域着色图像。如何在ios中找到uiimage中的透明区域?

这里我对色彩的图像代码

-(UIImage*)setCollor:(UIColor*)color image:(UIImage*)img 
{ 
    UIGraphicsBeginImageContext(img.size);  
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // set the fill color 
    [color setFill]; 

    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

先填充完整矩形的颜色。矩形将具有图像宽度和图像高度的大小。现在在同一矩形上绘制图像。

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rect); 

    CGContextDrawImage(context, rect, [UIImage imageNamed:@"myImage"].CGImage); 
+0

你可以给一些示例代码。 – banu

+0

我在做绘画app.so我想应用Floodfill算法 – banu

+0

你有没有任何示例代码 –