views:

734

answers:

1

I am trying to quickly invert the colors of an existing UIView (turn into a negative, as in a photographic negative). Or to take the colors and turn down the saturation. I think easier than manipulating the bitmap is to take another UIView that is all white(or shades), and to use blendmodes to achieve my goals.

I don't see a way to direct how a UIView is drawn on top of another. Even if I subclass UIView, I don't see an easy way to drawrect using a blend more. It looks like I will have to subclass UIImageView which has a drawrect:withBlendMode.

Has anyone done this in an easier way? I was hoping I could just set a blendmode property on a UIView or UIImageView and not have to subclass...

Basically, how would you quickly invert a view so that black is white and white is black?

+1  A: 

This seems to be the solution:

Crate a subclass of UIView. Then in drawRect:

    [self.image drawInRect:rect];

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

    // set the blend mode
    CGContextSetBlendMode(context, kCGBlendModeDifference);

    // now draw the appropriate color over the whole thing
    CGContextFillRect(....);

I'm off to try this out now.

mahboudz