IOS给图片添加水印(两种方式)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
IOS给图⽚添加⽔印(两种⽅式)
为了防⽌⾃⼰⾟苦做的项⽬被别⼈盗⾛,采取图⽚添加⽔印,在此表⽰图⽚的独⼀⽆⼆。
加⽔印不是在上⾯添加⼏个Label,⽽是我们把字画到图⽚上成为⼀个整体,下⾯⼩编给⼤家分享IOS给图⽚添加⽔印(两种⽅式)。
提供⼀个⽅法,此⽅法只需要传递⼀个要加⽔印的图⽚和⽔印的内容就达到效果。
第⼀种⽅式:
-(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name
{
NSString* mark = name;
int w = img.size.width;
int h = img.size.height;
UIGraphicsBeginImageContext(img.size);
[img drawInRect:CGRectMake(, , w, h)];
NSDictionary *attr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:], //设置字体
NSForegroundColorAttributeName : [UIColor redColor] //设置字体颜⾊
};
[mark drawInRect:CGRectMake(, , , ) withAttributes:attr]; //左上⾓
[mark drawInRect:CGRectMake(w - , , , ) withAttributes:attr]; //右上⾓
[mark drawInRect:CGRectMake(w - , h - - , , ) withAttributes:attr]; //右下⾓
[mark drawInRect:CGRectMake(, h - - , , ) withAttributes:attr]; //左下⾓
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}
第⼆种⽅式:⽤drawInRect很⽅便,图⽚、⽂字都可以加
// 画⽔印
- (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
{
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
}
#else
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
{
UIGraphicsBeginImageContext([self size]);
}
#endif
//原图
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
//⽔印图
[mask drawInRect:rect];
UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newPic;
}
以上叙述⽤两种⽅式实现IOS给图⽚添加⽔印,需要的朋友可以来参考下,希望⼤家能够喜欢。