【问题标题】:composite colors: CALayer and blend mode on iPhone复合颜色:iPhone 上的 CALayer 和混合模式
【发布时间】:2010-12-12 03:20:38
【问题描述】:

我正在尝试在 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];

}

有什么问题吗?

【问题讨论】:

    标签: iphone calayer blend mode


    【解决方案1】:

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

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

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

    【讨论】:

    • 如果你没有实现 calayer 委托函数 "- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context ",你应该使用 [mylayer renderInContext:c]
    【解决方案2】:

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

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

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

    【讨论】:

    • CoreImage 现在不能在 iOS 上使用吗?
    • @openfrog 是的,但与 OS X 相比它是有限的。例如,在 OS X 上,CALayers 可以有合成过滤器。在 iOS 上,没有那么多。
    【解决方案3】:

    一点都没有……我觉得用opengl来实现这个更容易,因为它似乎还没有在CA中实​​现。

    【讨论】:

      猜你喜欢
      • 2012-08-04
      • 2020-12-25
      • 2012-06-02
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2021-09-17
      • 2021-11-20
      相关资源
      最近更新 更多