iOS_Add Watermark to Picture_Tilt Text

Add a watermark to the picture, and the text is not inclined

// MARK:图片添加水印
+ (UIImage *)addWatermarketWithOriginImage:(UIImage *)originImage WaterText:(NSString *)waterText{
    UIGraphicsBeginImageContextWithOptions(originImage.size, NO, 0);
    // 绘制图片
    [originImage drawInRect:CGRectMake(0, 0, originImage.size.width, originImage.size.height)];
    // 添加水印
    if (waterText.length > 0) {
        NSDictionary *attributedDic =@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor grayColor],NSBackgroundColorAttributeName:[UIColor clearColor]};
        CGFloat pointX = 0;
        CGFloat pointY = 50;
        NSInteger index = 0;
        while (pointY < originImage.size.height) {
            [waterText drawAtPoint:CGPointMake(pointX, pointY) withAttributes:attributedDic];
            if (pointX < originImage.size.width) {
                pointX += 200;
            }else{
                pointX = -index*100;
                pointY += 150;
            }
            index++;
        }
    }
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Add watermark to the picture and the text is slanted

+ (UIImage *)addWatermarketWithOriginImage:(UIImage *)originImage WaterText:(NSString *)waterText{
    //原始image的宽高
    CGFloat viewWidth = originImage.size.width;
    CGFloat viewHeight = originImage.size.height;
    
    UIGraphicsBeginImageContextWithOptions(originImage.size, NO, 0);
    // 绘制图片
    [originImage drawInRect:CGRectMake(0, 0, viewWidth, viewHeight)];
    // 添加水印
    if (waterText.length > 0) {
        CGFloat horizontalSpace = 50;// 水平间隔
        CGFloat vertivalSpace = 100; // 竖直间隔
        NSDictionary *attributedDic =@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor grayColor],NSBackgroundColorAttributeName:[UIColor clearColor]};
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:waterText attributes:attributedDic];
        //绘制文字的宽高
        CGFloat strWidth = attrStr.size.width;
        CGFloat strHeight = attrStr.size.height;
        // 开始旋转上下文矩阵,绘制水印文字
        CGContextRef context = UIGraphicsGetCurrentContext();
        //将绘制原点(0,0)调整到源image的中心
        CGContextConcatCTM(context, CGAffineTransformMakeTranslation(viewWidth/2, viewHeight/2));
        //以绘制原点为中心旋转  (M_PI_2 / 3 ) <45>角度
        CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI_2 / 3));
    //将绘制原点恢复初始值,保证当前context中心和源image的中心处在一个点(当前context已经旋转,所以绘制出的任何layer都是倾斜的)
        CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-viewWidth/2, -viewHeight/2));
        
        // 对角线
        CGFloat sqrtLength = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
        //计算需要绘制的列数和行数
        int horCount = sqrtLength / (strWidth + horizontalSpace) + 1;
        int verCount = sqrtLength / (strHeight + vertivalSpace) + 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++) {
            [waterText drawInRect:CGRectMake(tempOrignX, tempOrignY, strWidth, strHeight) withAttributes:attributedDic];
            if (i % horCount == 0 && i != 0) {
                tempOrignX = orignX;
                tempOrignY += (strHeight + vertivalSpace);
            }else{
                tempOrignX += (strWidth + horizontalSpace);
            }
        }
    }
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Guess you like

Origin blog.csdn.net/FlyingKuiKui/article/details/105176146