// ViewController.m
// 图片擦除(纯代码)
// 如果想要要通过storyboard拖拽方式操作,拖两个imageview
// 其中用于做擦除操作的要连线,并给其拖一个手指移动的手势
#import "ViewController.h"
@interface ViewController ()
//保存用于做擦除操作的图片
@property (nonatomic,weak) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建两个imageview
//用于显示擦除后的图片
UIImageView *imageView1=[[UIImageView alloc] init];
imageView1.image=[UIImage imageNamed:@"2B"];
//设置ImageView可以和用户交互
imageView1.userInteractionEnabled=NO;
imageView1.frame=self.view.bounds;
[self.view addSubview:imageView1];
//用于做擦除操作的图片
UIImageView *imageView2=[[UIImageView alloc] init];
imageView2.image=[UIImage imageNamed:@"2A"];
imageView2.frame=self.view.bounds;
imageView2.userInteractionEnabled=YES;
_imageView=imageView2;
[self.view addSubview:imageView2];
//2.给用于做擦除操作的imageView添加手指移动手势
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
}
//3.监听手指移动事件
-(void) pan:(UIPanGestureRecognizer *)pan
{
//获取当前手指位置
CGPoint curP=[pan locationInView:_imageView];
//开启位图上下文
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size,NO, 0);
//绘制图片
[_imageView.image drawInRect:_imageView.bounds];
//获取当前上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//擦除范围大小
CGFloat wh=20;
CGRect clearRect= CGRectMake(curP.x, curP.y, wh, wh);
//擦除图片
CGContextClearRect(ctx, clearRect);
//生成新的图片
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
//给imageView赋值
_imageView.image=image;
}
@end
#import "ViewController.h"
@interface ViewController ()
//保存用于做擦除操作的图片
@property (nonatomic,weak) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建两个imageview
//用于显示擦除后的图片
UIImageView *imageView1=[[UIImageView alloc] init];
imageView1.image=[UIImage imageNamed:@"2B"];
//设置ImageView可以和用户交互
imageView1.userInteractionEnabled=NO;
imageView1.frame=self.view.bounds;
[self.view addSubview:imageView1];
//用于做擦除操作的图片
UIImageView *imageView2=[[UIImageView alloc] init];
imageView2.image=[UIImage imageNamed:@"2A"];
imageView2.frame=self.view.bounds;
imageView2.userInteractionEnabled=YES;
_imageView=imageView2;
[self.view addSubview:imageView2];
//2.给用于做擦除操作的imageView添加手指移动手势
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
}
//3.监听手指移动事件
-(void) pan:(UIPanGestureRecognizer *)pan
{
//获取当前手指位置
CGPoint curP=[pan locationInView:_imageView];
//开启位图上下文
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size,NO, 0);
//绘制图片
[_imageView.image drawInRect:_imageView.bounds];
//获取当前上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//擦除范围大小
CGFloat wh=20;
CGRect clearRect= CGRectMake(curP.x, curP.y, wh, wh);
//擦除图片
CGContextClearRect(ctx, clearRect);
//生成新的图片
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
//给imageView赋值
_imageView.image=image;
}
@end