码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 给照片加水印,文字

时间:2015-07-20 19:34:25      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:ios   uiimage   水印   文字   


+ (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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS 给照片加水印,文字

标签:ios   uiimage   水印   文字   

原文地址:http://blog.csdn.net/darongzi1314/article/details/46971081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!