给图片添加水印
添加水印前:
添加水印后:
+(UIImage*)getWatermarkWllpaper:(UIImage *)originImg waterImg:(UIImage *)waterImg{
//原始背景
UIImage *orgImg = originImg;
CGImageRef orgImgRef = orgImg.CGImage;
CGFloat orgWidth = CGImageGetWidth(orgImgRef);
CGFloat orgHeight = CGImageGetHeight(orgImgRef);
//水印图
UIImage *mrkImg = waterImg;
CGImageRef mrkImgRef = mrkImg.CGImage;
CGFloat markWidth = CGImageGetWidth(mrkImgRef);
CGFloat markHeight = CGImageGetHeight(mrkImgRef);
//右下角
UIImage *NameIDMixImg = [self getNameIDMixWater];
CGImageRef NameIDMixImgRef = NameIDMixImg.CGImage;
CGFloat NameIDMixWidth = CGImageGetWidth(NameIDMixImgRef);
CGFloat NameIDMixHeight = CGImageGetHeight(NameIDMixImgRef);
if (NameIDMixImg.scale>0) {
NameIDMixWidth = NameIDMixWidth/NameIDMixImg.scale;
NameIDMixHeight = NameIDMixHeight/NameIDMixImg.scale;
}
//以原始背景图大小为画布创建上下文
UIGraphicsBeginImageContext(CGSizeMake(orgWidth, orgHeight));
//关键代码
UIGraphicsBeginImageContextWithOptions(CGSizeMake(orgWidth, orgHeight), NO, [UIScreen mainScreen].scale);
[orgImg drawInRect:CGRectMake(0, 0, orgWidth, orgHeight)];//先把原始背景 画到上下文中
[mrkImg drawInRect:CGRectMake(0, (orgHeight-orgWidth*markHeight/markWidth)/2, orgWidth, orgWidth*markHeight/markWidth)];//再把水印放在上下文中
CGFloat mixShowWidth = orgWidth/3;
CGFloat mixSpace = orgWidth*0.02;
[NameIDMixImg drawInRect:CGRectMake(orgWidth-mixShowWidth-mixSpace, (orgHeight-mixShowWidth*NameIDMixHeight/NameIDMixWidth)-mixSpace, mixShowWidth, mixShowWidth*NameIDMixHeight/NameIDMixWidth)];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
UIGraphicsEndImageContext();//关闭上下文
return resultImg;
}
+(UIImage *)getNameIDMixWater{
//放大倍数
//反复测试(1-5)作用不大(即使要使用这个值也不能太大,生成会变慢),
//关键代码UIGraphicsBeginImageContextWithOptions(CGSizeMake(orgWidth, orgHeight), NO, [UIScreen mainScreen].scale)
CGFloat bigger = 1;
NSDictionary *textAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20*bigger]};
NSString *nameStr = [NSString stringWithFormat:@"作者:%@",[Config getOwnNicename]];
NSString *IDStr = [NSString stringWithFormat:@"%@号:%@",[self getAppName],[Config getOwnID]];
//文本
CGSize nameSize = [nameStr sizeWithAttributes:textAttributes];
CGSize IDSize = [IDStr sizeWithAttributes:textAttributes];
CGFloat name_id_mix_width = MAX(nameSize.width, IDSize.width);
CGFloat sigleHeight = 25*bigger;
CGFloat spaceRow = 5*bigger;
CGFloat name_id_mix_height = (sigleHeight*2+spaceRow);
CGRect textRect = CGRectMake(0, 0, name_id_mix_width, name_id_mix_height);
CGRect idRect = CGRectMake(name_id_mix_width - IDSize.width, sigleHeight+spaceRow, IDSize.width, IDSize.height);
CGRect nameRect = CGRectMake(name_id_mix_width - nameSize.width, 0, nameSize.width, nameSize.height);
UIGraphicsBeginImageContextWithOptions(textRect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// color
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
//阴影
CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 5*bigger, RGB_COLOR(@"#000000", 0.5).CGColor);
CGContextFillRect(context, textRect);
[IDStr drawInRect:idRect withAttributes:textAttributes];
[nameStr drawInRect:nameRect withAttributes:textAttributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}