作者:Mitchell
- Demo地址
- 业务逻辑:上下两张图片,上方遮盖,下方擦除之后想要显示的图片,为上方图片添加手势,根据手势的位置创建擦除的小块,然后开启位图,渲染被擦除的图层,清楚掉pan手势划过的区域,生成新的图片,关闭位图上下文,将新的图片赋给被擦除的图片。
CGPoint curP = [sender locationInView:sender.view];
- 确定擦除区域
CGFloat wh = 100;
CGRect clearR = CGRectMake(curP.x-wh*0.5, curP.y-wh*0.5, wh, wh);
- 开启位图上下文
UIGraphicsBeginImageContextWithOptions(sender.view.frame.size, NO, 0);
- 获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
- 渲染控件
[sender.view.layer renderInContext:ctx];
- 清除上下文某一部分
CGContextClearRect(ctx, clearR);
- 从上下文中生成一张图片
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
- 关闭上下文
UIGraphicsEndImageContext();
- 将生成的图片赋给覆盖的ImageView
UIImageView*imageV = (UIImageView*)sender.view;
imageV.image = image;