给图片添加水印

本文介绍了一种在iOS应用中为图片添加包含时间和坐标信息的水印的方法。通过使用UIKit框架下的UIImageView和UIImage,实现了将指定文本作为水印添加到图片上的功能,并展示了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *img = [UIImage imageNamed:@"gaoyuanyuan.jpg"];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, img.size.width - 50, img.size.height - 50)];
    imageV.image = [self waterImageWithText:[UIImage imageNamed:@"gaoyuanyuan.jpg"] textLogo:@"时间:2016-05-17 18:12:31 坐标:26.041369,119.219034"];
    [self.view addSubview:imageV];
    
    
    
}

- (UIImage *)waterImageWithText:(UIImage *)img textLogo:(NSString *)text1{
    //获取图片上下文
    CGSize size=CGSizeMake(img.size.width, img.size.height);
    UIGraphicsBeginImageContext(size);
    //获取当前上下文
    CGContextRef contextRef=UIGraphicsGetCurrentContext();
    NSLog(@"12312 %f %f",img.size.width,img.size.height);
    //转换矩阵
    
    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.5, -1.0);
    
    UIFont *font = [UIFont systemFontOfSize:20];
    UIColor *textColor = [UIColor whiteColor];
    UIColor *bgColor = kColor(0, 0, 0, 0.5);
    [text1 drawInRect:CGRectMake(0, img.size.height-28, [NSString widthAdaptWithContext:text1], 25) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,bgColor,NSBackgroundColorAttributeName,textColor,NSForegroundColorAttributeName, nil]];
    
    UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();//从当前上下文种获取图片
    
    UIGraphicsEndImageContext();
    
    return targetimg;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值