在两个UIView之间应用混合模式的最简单/最快的方法

mah*_*udz 9 iphone uiview uiimageview

我试图快速反转现有UIView的颜色(变成负面,如在摄影底片中).或者采取颜色并调低饱和度.我认为比操作位图更容易采取另一个全白(或阴影)的UIView,并使用blendmodes来实现我的目标.

我没有看到如何指导UIView如何在另一个之上绘制的方法.即使我是UIView的子类,我也没有看到使用混合更简单的方法.看起来我必须子类化具有drawrect的UIImageView:withBlendMode.

有没有人以更简单的方式做到这一点?我希望我可以在UIView或UIImageView上设置一个blendmode属性而不必子类...

基本上,你如何快速反转视图,使黑色为白色,白色为黑色?

mah*_*udz 7

这似乎是解决方案:

Crate是UIView的子类.然后在drawRect中:

    [self.image drawInRect:rect];

    // prepare the context to draw into
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the blend mode
    CGContextSetBlendMode(context, kCGBlendModeDifference);

    // now draw the appropriate color over the whole thing
    CGContextFillRect(....);
Run Code Online (Sandbox Code Playgroud)

我现在要试试这个.

  • 这实际上有用吗?我知道它随处可见,但有效吗? (6认同)