当前位置:首页 > 新闻中心 >

iOS 给照片加水印,文字

发布时间:2017-09-05 12:28:07 作者:陕西誉丰源物资有限公司

iOS 给照片加水印,文字

+ (instancetype)waterImageWithText:(UIImage *)img textLogoColor:(NSString *)text1{
/////注:此为后来更改,用于显示中文。zyq,2013-5-8
CGSize size = CGSizeMake(img.size.width, img.size.height); //设置上下文(画布)大小
UIGraphicsBeginImageContext(size); //创建一个基于位图的上下文(context),并将其设置为当前上下文
CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取当前上下文
CGContextTranslateCTM(contextRef, 0, img.size.height); //画布的高度
CGContextScaleCTM(contextRef, 1.0, -1.0); //画布翻转
CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), [img CGImage]); //在上下文种画当前图片
[[UIColor whiteColor] set]; //上下文种的文字属性
CGContextTranslateCTM(contextRef, 0, img.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
UIFont *font = [UIFont boldSystemFontOfSize:30];
//[text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font]; //此处设置文字显示的位置
//[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withFont:font];
UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext(); //从当前上下文种获取图片
UIGraphicsEndImageContext(); //移除栈顶的基于当前位图的图形上下文。
return targetimg;

}

+ (instancetype)waterImageWithImage:(UIImage *)bgImage logo:(NSString *)logo{

UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);// 1.创建一个基于位图的上下文

[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];// 2.画背景

UIImage *waterImage = [UIImage imageNamed:logo];// 3.画左下角的水印

CGFloat scale = 1;

CGFloat margin = 5;

CGFloat waterW = waterImage.size.width * scale;

CGFloat waterH = waterImage.size.height * scale;

CGFloat waterX = 0;

CGFloat waterY = bgImage.size.height - waterH - margin;

[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];


UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();//4.从上下文中取得制作完毕的UIImage对象

UIGraphicsEndImageContext();// 5.结束上下文、

return newImage;
}
+ (instancetype)waterImageWithText:(UIImage *)img textLogo:(NSString *)text1{

CGSize size=CGSizeMake(img.size.width, img.size.height);

UIGraphicsBeginImageContext(size);

CGContextRef contextRef=UIGraphicsGetCurrentContext();//上下文

//转换矩阵

CGContextTranslateCTM(contextRef, 0, img.size.height);//画布的高度.移动函数

CGContextScaleCTM(contextRef, 1.0, -1.0);//缩放函数

CGContextDrawImage(contextRef,CGRectMake(0,0,img.size.width,img.size.height),[img CGImage]);//在上下文种画当前图片

[[UIColor redColor] set]; //上下文种的文字属性

CGContextTranslateCTM(contextRef, 0, img.size.height);

CGContextScaleCTM(contextRef, 1.0, -1.0);

UIFont *font = [UIFont boldSystemFontOfSize:30];

[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];

UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();//从当前上下文种获取图片

UIGraphicsEndImageContext();

return targetimg;
}

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网络营销 http://www.feimao666.com

  • 上一篇:一个ZendOptimizer的困惑,让人不解-解决思路
  • 下一篇:最后一页