#import "UIImage+Watermark.h"
@implementation UIImage (Watermark)
- (UIImage *)watermarkImageWithText:(NSString *)text
{
//1.取到上下文
UIGraphicsBeginImageContext(self.size);
//2.绘制图片
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
//3.绘制水印文字
CGRect textRect = CGRectMake(0, self.size.height - 30, self.size.width, 30);
//设置样式
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
//设置属性
NSDictionary *attr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:25],
NSParagraphStyleAttributeName: style,
NSForegroundColorAttributeName: [UIColor whiteColor]
};
//绘制文字
[text drawInRect:textRect withAttributes:attr];
//4.获取到绘制完成的图片
UIImage *watermarkedImage = UIGraphicsGetImageFromCurrentImageContext();
//5.结束在图片上的绘制
UIGraphicsEndImageContext();
return watermarkedImage;
}
@end