#import "testWatermarkViewController.h"
#define HORIZONTAL_SPACE 30//水平间距
#define VERTICAL_SPACE 50//竖直间距
#define CG_TRANSFORM_ROTATION (M_PI_2 / 3)//旋转角度(正旋45度 || 反旋45度)
@interface testWatermarkViewController ()
@end
@implementation testWatermarkViewController
-(void)test{
NSLog(@"hello");
//1.加载原生图片
UIImage * draw = [UIImage imageNamed:@"background.png"];
UIImageView * drawView = [[UIImageView alloc]initWithImage:draw];
[drawViewsetFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:drawView];
//2.获取(开启)一个位图上下文,位图上下文与view无关联,所以不需要在drawRect方法中获取
// size:位图上下文的尺寸(新图片的尺寸)
// opaque: 不透明度 YES:不透明 NO:透明,通常我们一般都弄透明的上下文
// scale:通常不需要缩放上下文,取值为0,表示不缩放
UIGraphicsBeginImageContextWithOptions(draw.size, NO, 0);
//3.绘制原生图片
[drawdrawAtPoint:CGPointZero];
NSString *mark = @"waterMark";
//原始image的宽高
CGFloat viewWidth = draw.size.width;
CGFloat viewHeight = draw.size.height;
//为了防止图片失真,绘制区域宽高和原始图片宽高一样
UIGraphicsBeginImageContext(CGSizeMake(viewWidth, viewHeight));
//先将原始image绘制上
[drawdrawInRect:CGRectMake(0, 0, viewWidth, viewHeight)];
//sqrtLength:原始image的对角线length。在水印旋转矩阵中只要矩阵的宽高是原始image的对角线长度,无论旋转多少度都不会有空白。
CGFloat sqrtLength = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
//4.文字的属性
NSDictionary *attr = @{
//设置字体大小
NSFontAttributeName: [UIFont systemFontOfSize:23],
//设置文字颜色
NSForegroundColorAttributeName :[UIColor grayColor],
};
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:mark attributes:attr];
//绘制文字的宽高
CGFloat strWidth = attrStr.size.width;
CGFloat strHeight = attrStr.size.height;
//开始旋转上下文矩阵,绘制水印文字
CGContextRef context = UIGraphicsGetCurrentContext();
//将绘制原点(0,0)调整到源image的中心
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(viewWidth/2, viewHeight/2));
//以绘制原点为中心旋转
CGContextConcatCTM(context, CGAffineTransformMakeRotation(CG_TRANSFORM_ROTATION));
//将绘制原点恢复初始值,保证当前context中心和源image的中心处在一个点(当前context已经旋转,所以绘制出的任何layer都是倾斜的)
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-viewWidth/2, -viewHeight/2));
//计算需要绘制的列数和行数
int horCount = sqrtLength / (strWidth + HORIZONTAL_SPACE) + 1;
int verCount = sqrtLength / (strHeight + VERTICAL_SPACE) + 1;
//此处计算出需要绘制水印文字的起始点,由于水印区域要大于图片区域所以起点在原有基础上移
CGFloat orignX = -(sqrtLength-viewWidth)/2;
CGFloat orignY = -(sqrtLength-viewHeight)/2;
//在每列绘制时X坐标叠加
CGFloat tempOrignX = orignX;
//在每行绘制时Y坐标叠加
CGFloat tempOrignY = orignY;
for (int i = 0; i < horCount * verCount; i++) {
[markdrawInRect:CGRectMake(tempOrignX, tempOrignY, strWidth, strHeight) withAttributes:attr];
if (i % horCount == 0 && i != 0) {
tempOrignX = orignX;
tempOrignY += (strHeight +VERTICAL_SPACE);
}else{
tempOrignX += (strWidth +HORIZONTAL_SPACE);
}
}
//5.获取之后的图片
UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *finalImgView = [[UIImageView alloc]initWithImage:finalImg];
[finalImgViewsetFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:finalImgView];
//6.关闭上下文
UIGraphicsEndImageContext();
CGContextRestoreGState(context);
}
@end