您可以将两个 UIImageView 相互叠加,彩色版本在背景中,黑白版本在前景中。
然后您可以使用touchesBegan、touchesMoved 等事件来跟踪用户输入。在移动的触摸中,您可以像这样“擦除”用户移动手指的路径(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);。这会从图像中删除跟踪的部分,然后将图像设置为前景图像视图的图像。
当用户完成后,您应该能够将两个图像合并或使用黑白图像作为蒙版。