UIImage遮盖手势 [英] UIImage masking with gesture

查看:80
本文介绍了UIImage遮盖手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to achieve selective color feature in iOS. I personally think that first draw shape using finger gesture and convert that into mask, But at the same time it should be real time, It should work as i move my finger across the grayscale image. Can anyone direct me to correct path.

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

谢谢。

推荐答案

您可以将两个UIImageView彼此相对放置,背景颜色版本和黑白颜色

You can position two UIImageViews over each other, the color version in the background and the black&white version in the foreground.

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

Then you can use touchesBegan, touchesMoved and so on events to track user input. In touches moved you can "erase" a path that the user moved the finger along like this (self.foregroundDrawView is the black&white 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); 。这样会从图像中删除被跟踪的部分,然后将图像重新设置为前景图像视图的图像。

The important part is CGContextSetBlendMode(context, kCGBlendModeClear);. This erases the traced part from the image, afterwards the image is set back as the image of the foreground image view.

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

When the user is done you should be able to combine the two images or use the black&white image as a mask.

这篇关于UIImage遮盖手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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