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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值