UIImage掩盖与手势

我试图在iOS中实现select性的颜色function。 我个人认为,先用手指画出形状,然后将其转换成蒙版,但同时应该是实时的,它应该在我的手指在灰度图像上移动时工作。 任何人都可以指导我纠正path。

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

谢谢。

您可以将两个UIImageView放置在相互之间,背景中的颜色版本以及前景中的黑白版本。

然后你可以使用touchesBegantouchesMoved等事件来跟踪用户的input。 在触动移动你可以“擦除”一个用户像这样移动手指的path(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); 。 这将擦除图像中的跟踪部分,然后将图像设置为前景图像视图的图像。

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