Question

Hi Friends, I am developing color filling game.i know how to color in full image.but i want to color the image in transparent area.

Here my code for coloring image

-(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;
}

How to find transparent area in the image and fill the colors..?

Was it helpful?

Solution

First fill the color for complete rect. The rect will be of size image width and image height. Now draw the image on same rect.

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

    CGContextDrawImage(context, rect, [UIImage imageNamed:@"myImage"].CGImage);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top