views:

763

answers:

2

Hi,

I have a small image from a database and the image's average color need to be altered slightly.

It's a CGImageRef and I thought of creating a CGContext, drawing the image to this context, then subsequently changing the bitmap data somehow and finally rendering it. But how can I alter the color information?

Thanks for your help!

A: 

Check out this Apple Q&A on pixel data manipulation for details on how you might go about that.

Nick Veys
A: 

Draw a color onto the object like this:

    // first draw image
    [self.image drawInRect:rect];

    // prepare the context to draw into
 CGContextRef context = UIGraphicsGetCurrentContext();

 // set the blend mode and draw rectangle on top of image
 CGContextSetBlendMode(context, kCGBlendModeColor);
 CGContextClipToMask(context, self.bounds, image.CGImage); // this restricts drawing to within alpha channel
 CGContextSetRGBFillColor(context, 0.75, 0.0, 0.0, 1.0); // this is your color,  a light reddish tint
 CGContextFillRect(context, rect);

I put this into the drawRect: method of a custom UIView. That UIView has an ivar, UIImage *image that holds the image you want to tint or color.

willc2
Thanks, but I needed to change every pixel for its own.