图片擦除
添加一张图片,模仿橡皮擦功能
- (IBAction)pan:(UIPanGestureRecognizer *)sender {
// 获取当前触摸点 手势有一个View属性,View是添加了手势的view
CGPoint curP =[sender locationInView:sender.view];
// 确定擦除区域
CGFloat wh = 30;
CGFloat x = curP.x - wh * 0.5;
CGFloat y = curP.y - wh * 0.5;
CGRect clearR = CGRectMake(x, y, wh, wh);
// 开启位图上下文
UIGraphicsBeginImageContextWithOptions(sender.view.bounds.size, NO, 0);
// 获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 渲染控件
[sender.view.layer renderInContext:ctx];
// 清除上下文中某一部分
CGContextClearRect(ctx, clearR);
// 从上下文中生成一张图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
UIImageView *imageV = (UIImageView *)sender.view;
imageV.image = image;
}