【问题标题】:UIImage masking with gesture带有手势的 UIImage 遮罩
【发布时间】:2014-08-20 14:58:46
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: ios objective-c uiimage core-image


    【解决方案1】:

    您可以将两个 UIImageView 相互叠加,彩色版本在背景中,黑白版本在前景中。

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

        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);。这会从图像中删除跟踪的部分,然后将图像设置为前景图像视图的图像。

    当用户完成后,您应该能够将两个图像合并或使用黑白图像作为蒙版。

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 2023-03-18
      • 2019-05-26
      • 2014-05-23
      相关资源
      最近更新 更多