iOS:图片添加水印
来源:互联网 发布:晨曦预算软件 编辑:程序博客网 时间:2024/06/08 17:31
图片添加水印简单的理解就是将图片和文字绘制在一张图片内。值得注意的是,图片大小各异,即使添加相同样式的水印,也会在查看图片的时候,使水印看起来大小不一。为了解决这个问题,需要把图片按屏幕宽度重绘,水印添加的位置也需要是相对重绘后的图片的位置。
具体实现如下:
- (UIImage *)imageAddWaterMark:(NSString *)mark{ //针对屏幕重置图片大小 CGFloat ratio = self.size.height/self.size.width; CGFloat width = [UIScreen mainScreen].bounds.size.width; CGFloat height = width*ratio; //绘制到画布中 UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, [[UIScreen mainScreen] scale]); [self drawInRect:CGRectMake(0, 0, width, height)]; //转换成富文本 NSRange range = NSMakeRange(0,[mark length]); NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:mark]; //水印右对齐 NSMutableParagraphStyle *stype = [[NSMutableParagraphStyle alloc] init]; stype.alignment = NSTextAlignmentRight; [attributedString addAttribute:NSParagraphStyleAttributeName value:stype range:range]; //水印颜色 [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range]; //水印字体 [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:9] range:range]; //水印位置 CGRect rect = CGRectMake(-5, height-25, width, 25); [attributedString drawInRect:rect]; //获取水印后的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}
创建UIImage的类别,如:UIImage+Category.h
,即可直接使用。
NOTE
如果需要像新浪微博一样,添加的图片和文字水印,可使用富文本中的NSTextAttachment。
0 0
- iOS 图片添加水印
- ios图片添加水印
- iOS:图片添加水印
- iOS 图片添加文字水印
- ios图片添加文字或者水印
- [iOS]图片添加文字或者水印
- IOS给图片添加文字水印
- iOS在图片中添加水印
- iOS给图片添加水印 swift版本
- iOS截屏、给图片添加水印
- iOS 图片处理(截图,图片叠加(添加水印))
- Java添加水印+图片水印+文字水印
- 给图片添加水印
- 为图片添加水印
- 为图片添加水印
- 图片添加水印功能
- 给图片添加水印
- C# 添加 图片 水印
- 构造函数初始化时必须采用初始化列表的情况
- 51nod_1240 莫比乌斯函数
- java高级_set_ex
- 面向对象思想编程
- Spring中@Autowired注解用法
- iOS:图片添加水印
- 多线程--Interrupt
- Same Tree
- 一个大哥写的好东西,讲的很容易理解,特此记录
- HDU 5988 Coding Contest 2016青岛G题浮点费用流
- Markdown
- Android Project和app中两个build.gradle配置的区别
- submit不提交事件控制
- Cookie