提问者:小点点

UIImage使用手势遮罩


我正在尝试在iOS中实现选择性颜色功能。我个人认为首先使用手指手势绘制形状并将其转换为蒙版,但同时它应该是实时的,当我在灰度图像上移动手指时,它应该起作用。谁能指导我正确的路径。

示例应用程序 : https://itunes.apple.com/us/app/color-splash/id304871603?mt=8

谢谢。


共1个答案

匿名用户

您可以将两个UIImageViews放在彼此之上,彩色版本在背景中,黑色版本在

然后,您可以使用touchesBestartedtouchesMoved等事件来跟踪用户输入。在移动的触摸中,您可以像这样“擦除”用户移动手指的路径(self.foregroundDrawView 是黑色的

    UIGraphicsBeginImageContext(self.foregroundDrawView.frame.size);
    [self.foregroundDrawView.image drawInRect:CGRectMake(0, 0, self.foregroundDrawView.frame.size.width, self.foregroundDrawView.frame.size.height)];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetAllowsAntialiasing(context, TRUE);
    CGContextSetLineWidth(context, 85);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1.0);
    // Soft edge ... 5.0 works ok, but we try some more
    CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 13.0, [UIColor redColor].CGColor);

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, touchLocation.x, touchLocation.y);
    CGContextAddLineToPoint(context, currentLocation.x, currentLocation.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    self.foregroundDrawView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

重要的部分是CGContextSetBlendMode(context, kCGBlendModeClear);。这将从图像中擦除跟踪的部分,然后将图像设置为前景图像视图的图像。

用户完成后,您应该能够组合两个图像或使用黑色