简体   繁体   English

如何在Objective-C iOS中使用触摸删除图像?

[英]How to erase image by using touch in objective-c iOS?

I am working on a project where I have added a UIImage above another Image. 我正在一个项目中,我在另一个图像上方添加了一个UIImage。 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. 请为我建议一些示例代码或任何教程。

Thanks in advance 提前致谢

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 to erase the image. touchesMoved:事件移动到fingar删除图像。

-(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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM