图片添加文字水印

LEWatermark.h文件

#import 
#import 

NS_ASSUME_NONNULL_BEGIN

@interface LEWatermark : NSObject

/**
 添加字符串水印到图片,水印大小根据图片大小等比设置

 @param originalImage 需要添加水印的图片
 @param text 水印字符串
 @param scale 水印的大小比例,默认1
 @return 添加了水印的图片
 */
+ (UIImage *)watermardImageWithImage:(UIImage *)originalImage text:(NSString *)text watermardScale:(CGFloat)scale;

/**
 添加属性文本水印到图片

 @param originalImage 需要添加水印的图片
 @param title 水印属性文本
 @param horizontalSpace 水印水平间隔
 @param verticalSpace 水印竖直间隔
 @param angle 水印旋转弧度
 @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

// 水印按比例图片大小的比例设置
+ (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 = @{
                           // 设置字体大小
                           NSFontAttributeName : [UIFont systemFontOfSize:fontSize],
                           // 设置文字颜色
                           NSForegroundColorAttributeName : [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2],
                           // 设置阴影
                           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;
}

// 基础方法
+ (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;
    
    //为了防止图片失真,绘制区域宽高和原始图片宽高一样
    UIGraphicsBeginImageContext(CGSizeMake(viewWidth, viewHeight));
    //先将原始image绘制上
    [originalImage drawInRect:CGRectMake(0, 0, viewWidth, viewHeight)];
    //sqrtLength:原始image的对角线length。在水印旋转矩阵中只要矩阵的宽高是原始image的对角线长度,无论旋转多少度都不会有空白。
    CGFloat sqrtLength = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
    //绘制文字的宽高
    CGFloat strWidth = title.size.width;
    CGFloat strHeight = title.size.height;
    
    //开始旋转上下文矩阵,绘制水印文字
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //将绘制原点(0,0)调整到源image的中心
    CGContextConcatCTM(context, CGAffineTransformMakeTranslation(viewWidth/2, viewHeight/2));
    //以绘制原点为中心旋转
    CGContextConcatCTM(context, CGAffineTransformMakeRotation(angle));
    //将绘制原点恢复初始值,保证当前context中心和源image的中心处在一个点(当前context已经旋转,所以绘制出的任何layer都是倾斜的)
    CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-viewWidth/2, -viewHeight/2));
    
    //计算需要绘制的列数和行数
    int horCount = sqrtLength / (strWidth + horizontalSpace) + 1;
    int verCount = sqrtLength / (strHeight + verticalSpace) + 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++) {
        [title drawInRect:CGRectMake(tempOrignX, tempOrignY, strWidth, strHeight)];
        if (i % horCount == 0 && i != 0) {
            tempOrignX = orignX;
            tempOrignY += (strHeight + verticalSpace);
        }else{
            tempOrignX += (strWidth + horizontalSpace);
        }
    }
    //根据上下文制作成图片
    UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGContextRestoreGState(context);
    return finalImg;
}



@end

核心代码来自:https://www.jianshu.com/p/054468e95a11

你可能感兴趣的:(图片添加文字水印)