views:

842

answers:

4

I'm trying to use core image on the iphone. I'm able to composite my colors using quartz to draw an uiview, but i want to separate each component into CALayer (UIview consume more resources).

So i have a white mask i want to use to filter a background bitmap, and i want to try diffrent blending mode. UNfortunately, the layers are only "adding" their colors.

Here is my code :

@implementation WhiteLayerHelper

- (void)drawLayer:(CALayer *)theLayer
        inContext:(CGContextRef)myContext
{
    // draw a white overlay, with special blending and alpha values, so that the saturation can be animated
    CGContextSetBlendMode(myContext,kCGBlendModeSaturation);
    CGContextSetRGBFillColor(myContext,1.0,1.0,1.0,0.9);
    CGContextFillRect(myContext,[UIScreen mainScreen].bounds);

}

@end

And here is the main view drawrect code, where i use my CALayer :

- (void)drawRect:(CGRect)rect {
    //get the drawing context
    CGContextRef myContext = UIGraphicsGetCurrentContext();
    // draw the background
    [self fillContext:myContext withBounds:m_overlayRect withImage:m_currentImage];
    [whiteLayer renderInContext:myContext];

}

Is there something wrong ?

A: 

Did you find an answer? I also want to blend CALayers using CGContextSetBlendMode.

(Sorry, not enough rep to comment.)

Corin
A: 

Not at all... I think this is easier to use opengl to achieve this, because it seems to be not yet implemented in CA.

Ali
A: 

This method seems not to be a flaw of Core Animation, because the layers are prerendered into image contexts. Core Image is used for real time filtering (during animation and whatnot) of these images against background layers and their images. So the compositing properties of CALayer are used for this ability, which are not available on iPhone/iOS (yet) due to the requirement of Core Image.

OpenGL can do this for us in our situation, however =)

edit(add): setting the blend mode with CGContext in -drawInContext: and -drawLayer:inContext: does of course still have effect with what was already rendered or present in the image of that context. (when it is set before anything was rendered in the context('s image), it is the effect of blending against either full Black or full White (i am not sure which=)

humasect
+1  A: 

I managed to get the affect of compositing multiple CALayers by drawing them directly into a UIView's graphics context.

-(void)drawRect:(CGRect)rect {
 CGContextRef c = UIGraphicsGetCurrentContext();
 CGContextSetBlendMode(c, kCGBlendModeDifference);
 [myLayer drawInContext:c];
}

BTW, I did not add the layers as sublayers of the view's layer (that is I never called [myView.layer addSublayer:myLayer])

Logachu