iOS图片水印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
NSData *data = [photo fileDataRepresentation];
UIImage *img = [UIImage imageWithData:data];
//设置上下文(画布)大小
CGSize size = CGSizeMake(img.size.height, img.size.width) ;
//创建一个基于位图的上下文(context),并将其设置为当前上下文
UIGraphicsBeginImageContext(size);
//获取当前上下文
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//在上下文种画当前图片
CGContextDrawImage(contextRef, CGRectMake(0, 0,size.width , size.height), [img CGImage]);
NSString *title = @"1";
NSString *title1 = self.yzLeftTimeLab.text;
NSString *title2 = self.yzRightTimeLab.text;
NSString *title3 = self.yzWorkInfoLab.text;
[title drawInRect:CGRectMake(16, 20, 2, 55) withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:80],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[title1 drawInRect:CGRectMake(25, 15, 75, 40) withAttributes:@{NSFontAttributeName:[UIFont ej_pingFangSCMediumOfSize:28],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[title2 drawInRect:CGRectMake(110, 30, 100, 20) withAttributes:@{NSFontAttributeName:[UIFont ej_pingFangSCMediumOfSize:14],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[title3 drawInRect:CGRectMake(25, 55, VIEWWIDTH-50, 20) withAttributes:@{NSFontAttributeName:[UIFont ej_pingFangSCMediumOfSize:14],NSForegroundColorAttributeName:[UIColor whiteColor]}];

//从当前上下文种获取图片
UIImage *res =UIGraphicsGetImageFromCurrentImageContext();
//移除栈顶的基于当前位图的图形上下文
UIGraphicsEndImageContext();