+ (UIImage *)addWatermark:(UIImage *)image text:(NSString *)text { UIFont *font = [UIFont systemFontOfSize:32]; NSDictionary *attributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor colorWithRed:0 green:0 blue:0 alpha:0.01]}; UIImage *newImage = [image copy]; CGFloat x = 0.0; CGFloat y = 0.0; CGFloat idx0 = 0; CGFloat idx1 = 0; CGSize textSize = [text sizeWithAttributes:attributes]; while (y < image.size.height) { y = (textSize.height * 2) * idx1; while (x < image.size.width) { @autoreleasepool { x = (textSize.width * 2) * idx0; newImage = [self addWatermark:newImage text:text textPoint:CGPointMake(x, y) attributedString:attributes]; } idx0 ++; } x = 0; idx0 = 0; idx1 ++; } return newImage; }
+ (UIImage *)addWatermark:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary *)attributes {
UIGraphicsBeginImageContext(image.size); [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
CGSize textSize = [text sizeWithAttributes:attributes]; [text drawInRect:CGRectMake(point.x, point.y, textSize.width, textSize.height) withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return newImage; }
|
显示水印
通过上文提到的公式,可以让水印显示。
+ (UIImage *)visibleWatermark:(UIImage *)image {
|
代码和开源库
为了方便使用,写了一个开源库,封装的很实用,附带 DEMO
![]()
ZLYInvisibleWatermark
欢迎订阅我的个人公众号~╮(╯▽╰)╭
坚持原创技术分享,支持萌妹子,您的支持将鼓励我继续创作!
T