#import "UIImage+watermark.h"
#define RGB_COLOR(R,G,B)[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
@implementation UIImage (watermark)
- (UIImage *)watermarkImageWithText:(NSString *)text{
UIGraphicsBeginImageContext(self.size);//获取上下文
//绘制图片
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGRect rect = CGRectMake(0, self.size.height- 20, self.size.width, 20);
//段落格式
NSMutableParagraphStyle * style =[[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
style.alignment = NSTextAlignmentCenter;
int R = arc4random()%255;
int G = arc4random()%255;
int B = arc4random()%255;
//文字属性
NSDictionary *dic = @{
NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName:RGB_COLOR(R,G,B)};
//将文字绘制上去
[text drawInRect:rect withAttributes:dic];
//获取绘制到的图片
UIImage *watermarkImage = UIGraphicsGetImageFromCurrentImageContext();
//结束图片绘制
UIGraphicsEndImageContext();
return watermarkImage;
}