如何在objective-c iOS中使用touch来擦除图像? [英] How to erase image by using touch in objective-c iOS?

查看:129
本文介绍了如何在objective-c iOS中使用touch来擦除图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,我在另一个Image上面添加了一个UIImage。现在,我必须使用平滑的触摸来擦除顶部图像的某些部分或全部。我见过许多与我的相似的问题。但是在我的情况下,它们没有得到适当的回答或者可能无效。

I am working on a project where I have added a UIImage above another Image. Now, I have to erase some part or whole of that top image by using smooth touch. I have seen many other questions which are quite similar to mine. But they are not properly answered or might not worked in my case.

请为我推荐一些示例代码或任何教程。

Please suggest me some sample code or any tutorial for this.

提前致谢

推荐答案

创建图像视图.....

create image view.....

- (void)viewDidLoad
{
    eraseimg1 = [[UIImageView alloc] initWithFrame:CGRectMake(20,100, 80, 80)];
    eraseimg1.image = [UIImage imageNamed:@"qute.jpg"];
    [self.view  addSubview:eraseimg1];
    eraseimg1.userInteractionEnabled = YES;
    [super viewDidLoad];
}

touchesMoved: event to fingar move擦除图像。

touchesMoved: event to fingar move to erase the image.

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    UIGraphicsBeginImageContext(eraseimg1.frame.size);
    [eraseimg1.image drawInRect:CGRectMake(0, 0,eraseimg1.frame.size.width, eraseimg1.frame.size.height)];
    CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 50);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastTouch1.x, lastTouch1.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastTouch1.x, lastTouch1.y);
    CGContextAddRect(UIGraphicsGetCurrentContext(),CGRectMake(0,0,80,20));
    CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
    eraseimg1.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

这篇关于如何在objective-c iOS中使用touch来擦除图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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