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

发布于 2024-08-09 09:58:19 字数 1020 浏览 3 评论 0原文

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

}

有什么问题吗?

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 different 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送舟行 2024-08-16 09:58:19

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

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

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

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])

大姐,你呐 2024-08-16 09:58:19

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

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

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

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=)

铁轨上的流浪者 2024-08-16 09:58:19

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文