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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值