廢話不多說了准潭,添加文字水印的步驟如下:
1、創(chuàng)建一個(gè)UIImageView
-(instancetype)initWithFrame:(CGRect)frame withText:(NSString *)text
{
if (self = [super initWithFrame:frame]) {
self.alpha = 0.4; //設(shè)置水印透明
//添加水印 : Watermark 類文件 WaterImageWithImage:水印圖片 設(shè)置為 @""就是透明背景效果 text:傳入自己想要的水印文字
self.image = [Watermark view:self WaterImageWithImage:[UIImage imageNamed:@""] text:text];
}
return self;
}
2滑燃、創(chuàng)建類文件設(shè)置水印效果 Watermark
/ * 這三個(gè)屬性 主要是讓水印文字和水印文字之間間隔的效果惠猿,以及水印的文字的傾斜角度 ,不設(shè)置默認(rèn)為平行角度*/
#define HORIZONTAL_SPACEING 30//水平間距
#define VERTICAL_SPACEING 50//豎直間距
#define CG_TRANSFORM_ROTATING (M_PI_2 / 3)//旋轉(zhuǎn)角度(正旋45度 || 反旋45度)
@implementation Watermark
+(UIImage*)view:(UIImageView *)view WaterImageWithImage:(UIImage *)image text:(NSString *)text{
//設(shè)置水印大小望艺,可以根據(jù)圖片大小或者view大小
CGFloat img_w = view.bounds.size.width;
CGFloat img_h = view.bounds.size.height;
//1.開啟上下文
// UIGraphicsBeginImageContext(CGSizeMake(img_w, img_h));
UIGraphicsBeginImageContextWithOptions(CGSizeMake(w, h), NO, 0.0);
//2.繪制圖片 水印圖片
[image drawInRect:CGRectMake(0, 0, img_w, img_h)];
/* --添加水印文字樣式--*/
UIFont * font = [UIFont systemFontOfSize:23.0]; //水印文字大小
NSDictionary * attr = @{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor orangeColor]};
NSMutableAttributedString * attr_str =[[NSMutableAttributedString alloc]initWithString:text attributes:attr];
//文字:字符串的寬、高
CGFloat str_w = attr_str.size.width;
CGFloat str_h = attr_str.size.height;
//根據(jù)中心開啟旋轉(zhuǎn)上下文矩陣肌访,繪制水印文字
CGContextRef context = UIGraphicsGetCurrentContext();
//將繪制原點(diǎn)(0找默,0)調(diào)整到源image的中心
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(img_w/2, img_h/2));
//以繪制原點(diǎn)為中心旋轉(zhuǎn)
CGContextConcatCTM(context, CGAffineTransformMakeRotation(CG_TRANSFORM_ROTATING));
//將繪制原點(diǎn)恢復(fù)初始值,保證context中心點(diǎn)和image中心點(diǎn)處在一個(gè)點(diǎn)(當(dāng)前context已經(jīng)發(fā)生旋轉(zhuǎn)吼驶,繪制出的任何layer都是傾斜的)
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-w/2, -h/2));
//sqrtLength:原始image對(duì)角線length惩激。在水印旋轉(zhuǎn)矩陣中只要矩陣的寬高是原始image的對(duì)角線長(zhǎng)度,無論旋轉(zhuǎn)多少度都不會(huì)有空白蟹演。
CGFloat sqrtLength = sqrt(img_w*img_w + img_h*img_h);
//計(jì)算需要繪制的列數(shù)和行數(shù)
int count_Hor = sqrtLength / (str_w + HORIZONTAL_SPACEING) + 1;
int count_Ver = sqrtLength / (str_h + VERTICAL_SPACEING) + 1;
//此處計(jì)算出需要繪制水印文字的起始點(diǎn)风钻,由于水印區(qū)域要大于圖片區(qū)域所以起點(diǎn)在原有基礎(chǔ)上移
CGFloat orignX = -(sqrtLength-w)/2;
CGFloat orignY = -(sqrtLength-h)/2;
//在每列繪制時(shí)X坐標(biāo)疊加
CGFloat overlayOrignX = orignX;
//在每行繪制時(shí)Y坐標(biāo)疊加
CGFloat overlayOrignY = orignY;
for (int i = 0; i < count_Hor * count_Ver; i++) {
//繪制圖片
[text drawInRect:CGRectMake(overlayOrignX, overlayOrignY, str_w, str_h) withAttributes:attr];
if (i % count_Hor == 0 && i != 0) {
overlayOrignX = orignX;
overlayOrignY += (str_h + VERTICAL_SPACEING);
}else{
overlayOrignX += (str_w + HORIZONTAL_SPACEING);
}
}
//3.從上下文中獲取新圖片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//4.關(guān)閉圖形上下文
UIGraphicsEndImageContext();
CGContextRestoreGState(context);
return newImage;
}
@end
/
3.添加水印
Watermark_ImgView * wkImgView =[[Watermark_ImgView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) withText:@"自己想要的水印文字"];
// 想要水印不被任何UI遮擋住,就在最后酒请、最后骡技、最后的時(shí)候添加
[self.view addSubview:wkImgView];