使用Quartz2D技术擦除图片

#import "ViewController.h"

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    

    [self.view addGestureRecognizer:pan];

}

 

 

- (void)pan:(UIPanGestureRecognizer *)pan

{

    // 获取当前点

    CGPoint curP = [pan locationInView:self.view];

    

    // 获取擦除的矩形范围

    CGFloat wh = 60;

    CGFloat x = curP.x - wh * 0.5;

    CGFloat y = curP.y - wh * 0.5;

    

    CGRect rect = CGRectMake(x, y, wh, wh);

    

    // 开启上下文

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);

    

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 控件的layer渲染上去

    [_imageView.layer renderInContext:ctx];

    

    // 擦除图片

    CGContextClearRect(ctx, rect);

    

    // 生成一张图片

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    

    _imageView.image = image;

    

    // 关闭上下文

    UIGraphicsEndImageContext();

    

    

}

 

posted @ 2016-11-05 15:05  华少不思议  阅读(114)  评论(0编辑  收藏  举报