复合的颜色:在iPhone上的CALayer和混合模式(composite colors: CALa

2019-09-03 17:28发布

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

}

有什么不对?

Answer 1:

我设法直接把他们拉进一个UIView的图形上下文合成多个CALayers的影响。

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

顺便说一句,我没有所述层添加作为视图的层的子层(即我从来没有所谓的[myView.layer addSublayer:myLayer])



Answer 2:

这种方法似乎不是核心动画的一个缺陷,因为层预渲染成图像背景。 芯图像用于实时滤波对背景层和它们的图像,这些图像(动画和诸如此类的东西期间)。 所以CALayer的的合成属性用于这个能力,这并非适用于iPhone / iOS设备(还)由于核心形象的要求。

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

编辑(新增):设置混合模式与CGContext上在-drawInContext:和-drawLayer:inContext的:不,当然还有什么已经呈现或存在这方面的图像效果。 (当它是什么,之前设置的背景下(的形象)被渲染,这是对勾兑无论是全黑或全白的(我不知道哪个=)的影响



Answer 3:

并非在所有...我认为这是比较容易使用OpenGL来实现这一点,因为它似乎是尚未实现的CA.



文章来源: composite colors: CALayer and blend mode on iPhone