图片加标签地址:iOS 图片加标签
前言
勇~敢~的小伙伴儿们你们好哇,
我今天下午抽空把图片加水印的Demo做了,仿微博的,
但系呢,我忘记了以前有没有写过这方面内容,所以不细谈,主要东西都在代码里了,
所以正文就是代码咯~
github的Demo地址:图片加水印(仿微博)
正文
1、代码展示
//给图片加文字水印和图片水印 仿微博 可自行修改
-(UIImage *)watermarkImage:(UIImage *)img withWatermarkImage:(UIImage *)watermarkImage withWatermarkString:(NSString *)watermarkString withFontSize:(CGFloat)fontSize
{
int w = img.size.width;
int h = img.size.height;
//铺一张空白画布 原图size
UIGraphicsBeginImageContext(img.size);
//将原图画在画布上
[img drawInRect:CGRectMake(0,0 , w, h)];
//设置段落格式为局右对齐
NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init];
paragraph.alignment=NSTextAlignmentRight;
//设置字体字典
NSDictionary *attr = @{
NSFontAttributeName: [UIFont systemFontOfSize:fontSize], //设置字体
NSForegroundColorAttributeName : [UIColor whiteColor ], //设置字体颜色
NSParagraphStyleAttributeName:paragraph //设置段落格式
};
//获取字符串按照格式得到的Size
CGSize stringSize = [watermarkString sizeWithAttributes:attr];
//将水印画在画布右下角(可自行调整),按照它的size
[watermarkString drawInRect:CGRectMake(w - stringSize.width, h - stringSize.height,stringSize.width,stringSize.height) withAttributes:attr];
//将水印图片画在文字水印的前面
[watermarkImage drawInRect:CGRectMake(w - stringSize.width - stringSize.height, h - stringSize.height, stringSize.height, stringSize.height)];
//画完时候存成新的Image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//结束绘画
UIGraphicsEndImageContext();
return newImage;
}