复合颜色:iPhone上的CALayer和混合模式

Ali*_*Ali 6 iphone blend mode calayer

我正试图在iphone上使用核心图像.我能够使用quartz来合成我的颜色以绘制uiview,但我想将每个组件分开CALayer(UIview消耗更多资源).

所以我有一个白色的面具我想用来过滤背景位图,我想尝试不同的混合模式.不幸的是,这些图层只是"添加"了它们的颜色.

这是我的代码:

@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
Run Code Online (Sandbox Code Playgroud)

这是drawrect我使用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];

}
Run Code Online (Sandbox Code Playgroud)

有什么不对?

Log*_*chu 7

通过直接将它们绘制到UIView的图形上下文中,我设法获得了合成多个CALayers的效果.

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

顺便说一下,我没有将图层添加为视图图层的子图层(我从未调用[myView.layer addSublayer:myLayer])

  • 如果你没有实现calayer委托函数" - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context",你应该使用[mylayer renderInContext:c] (5认同)

hum*_*ect 5

这种方法似乎不是 Core Animation 的缺陷,因为层被预渲染到图像上下文中。Core Image 用于根据背景层及其图像对这些图像进行实时过滤(在动画期间等)。所以 CALayer 的合成属性用于此功能,由于 Core Image 的要求,这些属性在 iPhone/iOS 上(尚)不可用。

然而,OpenGL 可以在我们的情况下为我们做到这一点 =)

编辑(添加):在 -drawInContext: 和 -drawLayer:inContext: 中使用 CGContext 设置混合模式当然仍然对已经渲染或存在于该上下文图像中的内容有效。(当它在上下文(的图像)中呈现任何内容之前设置时,它是与全黑或全白混合的效果(我不确定哪个=)


小智 1

完全不是……我觉得这个用opengl来实现这个更容易一些,因为CA里好像还没有实现。