11 Replies Latest reply: Dec 15, 2009 11:36 PM by geekkid
Bridgeyman Level 1 Level 1 (0 points)
I am trying to color a given image from within my program. It is just a white image, so I want to replace all the white pixels with some other given color. Either doing this using UIViews or CALayers would work for my needs.

Core Image filters are not available on the iPhone, so no luck there.

I could also use the image as a mask on a CALayer, and then set the backgroundcolor of the CALayer to acheive the desired effect, but the mask property doesn't seem available on the iPhone (although it does show up in an API search).

Any help would be greatly appreciated.

Thank You!
Bridger

Macbook Intel Core Duo 2.0GHz, Mac OS X (10.5.4)
  • 1. Re: [iPhone] Color White Image
    RickMaddy Level 4 Level 4 (1,320 points)
    Here's a utility method I have to colorize a gray scaled image.


    + (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);

    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);

    [theColor set];
    CGContextFillRect(ctx, area);

    CGContextRestoreGState(ctx);

    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

    CGContextDrawImage(ctx, area, baseImage.CGImage);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
    }
  • 2. Re: [iPhone] Color White Image
    orangekay Level 5 Level 5 (4,085 points)
    What exactly is a "white image?" If it's just a white bitmap then draw whatever color rectangle you want it to be.
  • 3. Re: [iPhone] Color White Image
    Bridgeyman Level 1 Level 1 (0 points)
    It is a starburst type image, with only the color white and transparency to make the shape. I will try out RickMaddy's code sample, it looks exactly like what I was looking for.
  • 4. Re: [iPhone] Color White Image
    orangekay Level 5 Level 5 (4,085 points)
    So if the alpha channel is responsible for the shape you wish to draw it doesn't matter what color pixels are in the bitmap. Just paint whatever color you want.
  • 5. Re: [iPhone] Color White Image
    lamer0k Level 1 Level 1 (0 points)
  • 6. Re: [iPhone] Color White Image
    RickMaddy Level 4 Level 4 (1,320 points)
    FYI - You have a lot of memory leaks in that code.
  • 7. Re: [iPhone] Color White Image
    lamer0k Level 1 Level 1 (0 points)
  • 8. Re: [iPhone] Color White Image
    bicode Level 1 Level 1 (0 points)
    --Rick
    Thanks for the code. It was a huge help. For some reason when I colorize the images however they have a faint white outline. Any suggestions?
  • 9. Re: [iPhone] Color White Image
    chuty Level 1 Level 1 (0 points)
    I changed ur code like this



    This code fill the image with green color only.

    Can we fill the image with different color?

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UIColor *greenColor, blueColor,redColor;
    UIColor *theColor1;



    if(button == @"green" || button ==@"red")
    {



    NSLog(@"I am Touch");

    UITouch *touch = [[event allTouches] anyObject];
    if([touch view] == baseImage1)
    {
    NSLog(@"touch");
    image1=baseImage1;
    }

    if([touch view] == baseImage)
    {
    NSLog(@"baseImage");
    image1=baseImage;
    }




    NSLog(@"image=%@",image1);

    //UIColor *greenColor;

    CGImageRef imageRef = self.image1.image.CGImage;
    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
    size_t imageWidth = CGImageGetWidth(imageRef);
    size_t imageHeight = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
    //CGColorSpaceRef colorSpace =CGColorSpaceGetNumberOfComponents(imageRef);







    CFDataRef dr = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
    UInt8* dataPtr = (UInt8*)CFDataGetBytePtr(dr);
    int max = bytesPerRow * imageHeight;

    for (int i = 0; i < max; i++)
    {

    if (0 != dataPtr[i + 3])
    {
    //NSInteger res=dataPtr[i + 3];
    //NSLog(@"dataPtr[i + 3]=%d",res);

    dataPtr[i++]=(int)redColor;

    }
    else
    i += 3;



    }

    CGContextRef ref = CGBitmapContextCreate(dataPtr, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, colorSpace,kCGImageAlphaNoneSkipLast);//To set the new image

    if (nil == ref)
    NSLog(@"Error creating context!");
    CGImageRef imageRefNew = CGBitmapContextCreateImage(ref);

    UIImage *img = [UIImage imageWithCGImage:imageRefNew];
    self.image1.image = img;


    CFRelease(imageRefNew);
    CFRelease(ref);
    CFRelease(colorSpace);
    CFRelease(dr);
    return self;
    }

    }


    judy
  • 10. Re: [iPhone] Color White Image
    OSMVP Level 1 Level 1 (0 points)
    the code you people have written here will color the whole image..... can anyone tell me how to color certain shape on a image?? let say if i had a star or glass in the image how can i color only them not the whole image....
  • 11. Re: [iPhone] Color White Image
    geekkid Level 1 Level 1 (0 points)
    Rick,

    This works brilliantly for what I want to do, but for one problem; the image gets rotated by 270 degrees in the output. Can you please help?

    Gaury