开始先引入UIKit框架
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self waterImage];
[self clearUpImage];
}
/** 刮刮乐 */
- (void)clearUpImage{
//图片透明
//添加图片
UIImageView *v2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
v2.frame = self.view.frame;
[self.view addSubview:v2];
UIImageView *v1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
v1.frame = self.view.frame;
[self.view addSubview:v1];
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[v1 addGestureRecognizer:pan];
v1.userInteractionEnabled = YES;
}
- (void)panAction:(UIGestureRecognizer *)sender{
//获取当前点
CGPoint currentPoint = [sender locationInView:sender.view];
//设置擦除范围
CGFloat w = 50;
CGRect clearR = CGRectMake(currentPoint.x - w / 2, currentPoint.y - w / 2, w, w);
//开启图片上下文
UIGraphicsBeginImageContextWithOptions(sender.view.frame.size, NO, 0);
//获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//将图片提交给上下文
[sender.view.layer renderInContext:ctx];
//擦除
CGContextClearRect(ctx, clearR);
//获取擦除后的图片
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
//赋值
UIImageView *imv = (UIImageView *)sender.view;
imv.image = i;
//结束上下文
UIGraphicsEndImageContext();
}
/** 图片打水印 */
- (void)waterImage{
//获取图片
UIImage *image = [UIImage imageNamed:@"1.jpg"];
//开启图形上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//绘制图片
[image drawAtPoint:CGPointZero];
//准备水印内容
//文字
NSString *str = @"爽爽出品";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];//改写字体属性
dict[NSFontAttributeName] = [UIFont systemFontOfSize:30];//字号
dict[NSForegroundColorAttributeName] = [UIColor blueColor];//颜色
dict[NSStrokeWidthAttributeName] = @5;//空心
[str drawInRect:CGRectMake(250, 380, 200, 50) withAttributes:dict];
//图片
UIImage *star = [UIImage imageNamed:@"star_faved"];
[star drawInRect:CGRectMake(200, 380, 50, 50)];
//生成一个新图片
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *imv = [[UIImageView alloc]initWithImage:i];
imv.frame = self.view.frame;
[self.view addSubview:imv];
}
@end