如何在CGBitmapContext中实现软橡皮擦笔划 [英] How do I implement a soft eraser stroke in a CGBitmapContext

查看:172
本文介绍了如何在CGBitmapContext中实现软橡皮擦笔划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS中使用我的触控功能擦除图片。通过设置混合模式为kCGBlendModeClear我可以做到这一点 - 但只有硬边缘。我可以绘制我的笔画与不同的线宽和alphas,但它似乎CGContextSetAlpha没有影响与kCGBlendModeClear。



如何解决这个问题?

解决方案

透明层与 kCGBlendModeDestinationOut Da *(1-Sa),Dc *(1-Sa)类似这样的:

  CGPathRef pathToErase = ...; //你想擦除的路径
//也可以是一个图像或(几乎)任何其他的
//可以在位图上下文中绘制

CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
CGContextBeginTransparencyLayer(ctx,NULL);
{
CGContextSetGrayFillColor(ctx,0.0,1.0); // solid black

CGContextAddPath(ctx,pathToErase);
CGContextFillPath(ctx);
//上面两行可以改为CGContextDrawImage()
//或任何其他你用来清除
}
CGContextEndTransparencyLayer(ctx);请注意,您还应该保存和恢复gstate( CGContextSaveGState()

/ code> / / CGContextRestoreGState()),以确保混合模式和任何其他gstate更改不会持久。 b
$ b

注意:这是大脑编译的,透明层可能不会在所有混合模式下播放。如果是,请尝试将路径/图片绘制到第二个位图上下文中,然后使用上述混合模式绘制该上下文的内容。



您也可以尝试其他混合不同效果的模式。


I am trying to erase an image with my touch in iOS. By setting the blend mode to kCGBlendModeClear I am able to do this - but only with hard edges. I could draw my stroke with varying line widths and alphas, but it appears that CGContextSetAlpha does not have an effect with kCGBlendModeClear.

How do I go about this?

解决方案

I would use a transparency layer compositing with kCGBlendModeDestinationOut (Da * (1 - Sa), Dc * (1 - Sa).) Something like this:

CGPathRef pathToErase = ...; // The path you want erased
// could also be an image or (nearly) anything else
// that can be drawn in a bitmap context

CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
CGContextBeginTransparencyLayer(ctx, NULL);
{
    CGContextSetGrayFillColor(ctx, 0.0, 1.0); // solid black

    CGContextAddPath(ctx, pathToErase);
    CGContextFillPath(ctx);
    // the above two lines could instead be CGContextDrawImage()
    // or whatever else you're using to clear
}
CGContextEndTransparencyLayer(ctx);

Note that you should also save and restore the gstate (CGContextSaveGState()/CGContextRestoreGState()) before/after the transparency layer to ensure that the blend mode and any other gstate changes do not persist.

Note: This is brain-compiled and it's possible that transparency layers don't play nice with all blend modes. If so, try drawing the path/image into a second bitmap context, then drawing the contents of that context with the above blend mode.

You can also experiment with other blend modes for different effects.

这篇关于如何在CGBitmapContext中实现软橡皮擦笔划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆