LEWatermark.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface LEWatermark : NSObject
/**
添加字符串水印到圖片,水印大小根據(jù)圖片大小等比設(shè)置
@param originalImage 需要添加水印的圖片
@param text 水印字符串
@param scale 水印的大小比例,默認(rèn)1
@return 添加了水印的圖片
*/
+ (UIImage *)watermardImageWithImage:(UIImage *)originalImage text:(NSString *)text watermardScale:(CGFloat)scale;
/**
添加屬性文本水印到圖片
@param originalImage 需要添加水印的圖片
@param title 水印屬性文本
@param horizontalSpace 水印水平間隔
@param verticalSpace 水印豎直間隔
@param angle 水印旋轉(zhuǎn)弧度
@return 添加了水印的圖片
*/
+ (UIImage *)watermardImageWithImage:(UIImage *)originalImage watermark:(NSAttributedString *)title horizontalSpace:(CGFloat)horizontalSpace verticalSpace:(CGFloat)verticalSpace angle:(CGFloat)angle;
@end
NS_ASSUME_NONNULL_END
LEWatermark.m文件
#import "LEWatermark.h"
@implementation LEWatermark
// 水印按比例圖片大小的比例設(shè)置
+ (UIImage *)watermardImageWithImage:(UIImage *)originalImage text:(NSString *)text watermardScale:(CGFloat)scale {
CGFloat viewWidth = originalImage.size.width;
CGFloat viewHeight = originalImage.size.height;
CGFloat min = MIN(viewWidth, viewHeight);
CGFloat inScale = min / 1242 * scale;
CGFloat horizzontalSpace = 200.0 * inScale;
CGFloat verticalSpace = 150.0 * inScale;
CGFloat angle = (M_PI_2/ -2);
CGFloat fontSize = MAX(10, (70 * inScale));
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeMake(5 * inScale, 8 * inScale)];
shadow.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
//文字的屬性
NSDictionary *attr = @{
// 設(shè)置字體大小
NSFontAttributeName : [UIFont systemFontOfSize:fontSize],
// 設(shè)置文字顏色
NSForegroundColorAttributeName : [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2],
// 設(shè)置陰影
NSShadowAttributeName : shadow
};
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attr];
UIImage *image = [LEWatermark watermardImageWithImage:originalImage watermark:attrStr horizontalSpace:horizzontalSpace verticalSpace:verticalSpace angle:angle];
return image;
}
// 基礎(chǔ)方法
+ (UIImage *)watermardImageWithImage:(UIImage *)originalImage watermark:(NSAttributedString *)title horizontalSpace:(CGFloat)horizontalSpace verticalSpace:(CGFloat)verticalSpace angle:(CGFloat)angle {
if (!originalImage) {
return originalImage;
}
if (!title || title.length == 0) {
return originalImage;
}
//原始image的寬高
CGFloat viewWidth = originalImage.size.width;
CGFloat viewHeight = originalImage.size.height;
//為了防止圖片失真,繪制區(qū)域?qū)捀吆驮紙D片寬高一樣
UIGraphicsBeginImageContext(CGSizeMake(viewWidth, viewHeight));
//先將原始image繪制上
[originalImage drawInRect:CGRectMake(0, 0, viewWidth, viewHeight)];
//sqrtLength:原始image的對(duì)角線length。在水印旋轉(zhuǎn)矩陣中只要矩陣的寬高是原始image的對(duì)角線長(zhǎng)度,無(wú)論旋轉(zhuǎn)多少度都不會(huì)有空白。
CGFloat sqrtLength = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
//繪制文字的寬高
CGFloat strWidth = title.size.width;
CGFloat strHeight = title.size.height;
//開(kāi)始旋轉(zhuǎn)上下文矩陣,繪制水印文字
CGContextRef context = UIGraphicsGetCurrentContext();
//將繪制原點(diǎn)(0,0)調(diào)整到源image的中心
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(viewWidth/2, viewHeight/2));
//以繪制原點(diǎn)為中心旋轉(zhuǎn)
CGContextConcatCTM(context, CGAffineTransformMakeRotation(angle));
//將繪制原點(diǎn)恢復(fù)初始值,保證當(dāng)前context中心和源image的中心處在一個(gè)點(diǎn)(當(dāng)前context已經(jīng)旋轉(zhuǎn),所以繪制出的任何layer都是傾斜的)
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-viewWidth/2, -viewHeight/2));
//計(jì)算需要繪制的列數(shù)和行數(shù)
int horCount = sqrtLength / (strWidth + horizontalSpace) + 1;
int verCount = sqrtLength / (strHeight + verticalSpace) + 1;
//此處計(jì)算出需要繪制水印文字的起始點(diǎn),由于水印區(qū)域要大于圖片區(qū)域所以起點(diǎn)在原有基礎(chǔ)上移
CGFloat orignX = -(sqrtLength-viewWidth)/2;
CGFloat orignY = -(sqrtLength-viewHeight)/2;
//在每列繪制時(shí)X坐標(biāo)疊加
CGFloat tempOrignX = orignX;
//在每行繪制時(shí)Y坐標(biāo)疊加
CGFloat tempOrignY = orignY;
for (int i = 0; i < horCount * verCount; i++) {
[title drawInRect:CGRectMake(tempOrignX, tempOrignY, strWidth, strHeight)];
if (i % horCount == 0 && i != 0) {
tempOrignX = orignX;
tempOrignY += (strHeight + verticalSpace);
}else{
tempOrignX += (strWidth + horizontalSpace);
}
}
//根據(jù)上下文制作成圖片
UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextRestoreGState(context);
return finalImg;
}
@end
核心代碼來(lái)自:http://www.itdecent.cn/p/054468e95a11