6

我正在尝试在 iphone 上使用核心图像。我可以使用石英合成我的颜色来绘制一个 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

这是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];

}

有什么不对?

4

3 回答 3

7

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

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

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

于 2010-08-15T04:01:39.743 回答
5

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

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

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

于 2010-07-13T20:21:00.603 回答
1

一点也不...我认为使用opengl来实现这一点更容易,因为它似乎还没有在CA中实​​现。

于 2010-06-01T13:27:49.367 回答