0

我正在做一个项目,我在另一个图像上方添加了一个 UIImage。现在,我必须使用平滑的触摸来擦除顶部图像的一部分或全部。我看到了许多与我的非常相似的其他问题。但他们没有得到正确的回答,或者在我的情况下可能不起作用。

请为此建议我一些示例代码或任何教程。

提前致谢

4

1 回答 1

0

创建图像视图.....

- (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:事件手指移动以擦除图像。

-(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();
}
于 2016-05-19T04:35:53.197 回答