UIImage 添加水印

- (UIImage *)showWaterMaskInImage:(UIImage *)img WithText:(NSString *)text withlogoImage:(NSString *)waterMarkImg {
float heightScale = SCREEN_HEIGHT/img.size.height/1.0;//图片的高度和屏幕高度的比例
float widthScale = SCREEN_WIDTH/img.size.width/1.0;//图片的宽和屏幕宽的比例
float scale = MIN(heightScale, widthScale);//选取比较小的比例
float h = img.size.height*scale;//图片的高度乘以比例系数 == 水印图片的高度
float w = img.size.width*scale;//图片的宽度乘以比例系数 == 水印图片的宽
float fitScale = img.size.width/w;
CGSize newFitSize = CGSizeMake(w*fitScale, h*fitScale);
//生成要获取image的区域
UIGraphicsBeginImageContext(newFitSize);
// 获取当前图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 向下移动该图形上下文的座标原点
CGContextTranslateCTM(context, 0, newFitSize.height);
// 向上翻转图形上下文
CGContextScaleCTM(context, 1.0, -1.0);
// 创建显示区域
CGRect imageframe = CGRectMake(0, 0, newFitSize.width, newFitSize.height);
// 绘制图片
CGContextDrawImage(context, imageframe, img.CGImage);
[[UIColor darkGrayColor] set];//上下文种的文字属性
CGContextTranslateCTM(context, 0, newFitSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
UIFont *font = [UIFont boldSystemFontOfSize:12*fitScale];
//此处设置文字显示的位置
[text drawInRect:CGRectMake(newFitSize.width - 95*fitScale, newFitSize.height - 22*fitScale, 90*fitScale, 12*fitScale) withFont:font];
// [[UIImage imageNamed:waterMarkImg] drawInRect:CGRectMake(newFitSize.width - 115*fitScale, newFitSize.height - 22*fitScale, 15*fitScale, 15*fitScale)];
// 从当前上下文种获取图片
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
//移除栈顶的基于当前位图的图形上下文。
UIGraphicsEndImageContext();
return image;
}

不用尺寸的图片上显示的水印是一样的尺寸

原文地址:https://www.cnblogs.com/zrr-notes/p/7489835.html