UIImage添加水印(Logo+文字)
来源:互联网 发布:软件设计师考试书籍 编辑:程序博客网 时间:2024/04/28 20:04
写在前面
添加水印是经常遇到的需求了,也算是图像数字处理比较容易的一个环节,网上能搜出好几种解决方案,但作为新手的我还是折腾了好长时间。
简单介绍
没有什么逻辑,就是把你所需要用到的素材全都渲染到contex中,最后再作为一个整体取出来。
// 创建一个bitmap的contextUIGraphicsBeginImageContext();// 渲染背景图// 渲染素材logo+文字// 用的是同一个方法drawInRect:. . .// 取出UIImageUIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();//一些释放操作UIGraphicsEndImageContext();
应用
由于是UIImage的一些处理,这种需求最合适写进category里面
- (UIImage *)imageWater1:(UIImage *)imageLogo waterString:(NSString *)waterString{ NSUInteger inputWidth = self.size.width; // 创建一个bitmap的context UIGraphicsBeginImageContext(self.size);// 开始图片渲染 [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];// logo渲染 CGFloat ghostImageAspectRatio = imageLogo.size.width / imageLogo.size.height; NSInteger targetGhostWidth = inputWidth * 0.3; [imageLogo drawInRect:CGRectMake(0, 0, targetGhostWidth, targetGhostWidth/ghostImageAspectRatio)];// 渲染文字 NSUInteger wordHigh = 120; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping; NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:100], NSForegroundColorAttributeName:[UIColor whiteColor], NSParagraphStyleAttributeName:paragraphStyle}; [waterString drawInRect:CGRectMake(targetGhostWidth, 0, self.size.width*0.6, wordHigh) withAttributes:dic]; [@"RNO:0000023034001230" drawInRect:CGRectMake(targetGhostWidth, wordHigh*1, self.size.width*0.6, wordHigh) withAttributes:dic]; [@"GPS:27.34223132,4533.2313324" drawInRect:CGRectMake(targetGhostWidth, wordHigh*2, self.size.width*0.6, wordHigh) withAttributes:dic]; [@"地址:需要您的同意,才能访问相册" drawInRect:CGRectMake(targetGhostWidth, wordHigh*3, self.size.width*0.6, wordHigh) withAttributes:dic];// UIImage UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageNew;}
这样写是OK的,刚开始在渲染logo是这样写的
[imageLogo drawInRect:CGRectMake(0, 0, imageLogo.size.width, imageLogo.size.height)];
渲染出来的 图片特小,因为是位图(bitmap)的关系吧,
要想用logo实际大小可以这样
CGImageRef logoCGImg = [imageLogo CGImage];CGFloat w = CGImageGetWidth(logoCGImg);CGFloat h = CGImageGetHeight(logoCGImg);[imageLogo drawInRect:CGRectMake(0, 0, w, h)];
0 0
- UIImage添加水印(Logo+文字)
- UIImage 添加文字(水印)
- DotNet为图片添加水印(LOGO及文字)
- 添加水印logo和文字 iOS
- ASP.NET 上传图片添加文字、Logo水印
- ffmpeg 添加水印LOGO
- Java添加水印(图片水印,文字水印)
- Java添加水印(图片水印,文字水印)
- ffmpeg 视频添加水印 logo
- Java添加水印+图片水印+文字水印
- 添加水印(文字and图片)
- Asp添加文字水印
- js添加水印文字
- 文字水印添加
- 添加文字水印
- JS添加文字水印
- java 添加文字水印
- 添加水印文字
- window tomcat集群(亲测)
- addEventListener/attachEvent兼容IE浏览器与标准浏览器
- linux打包压缩命令
- linux 下的系统服务管理
- hdu 4609 3-idiots [FFT计数]【数学】
- UIImage添加水印(Logo+文字)
- OJ用java时总出现Time Limit Exceeded原因之一
- vim 使用
- Win10下,笔记本通过网线分享无线网络
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- ASP.NET MVC架构与实战系列之二:理解MVC路由配置
- JQuery DataTable 详解
- 集合框架_Collections工具类的概述
- Note01