美文网首页
32.给图片添加水印

32.给图片添加水印

作者: bytebytebyte | 来源:发表于2020-11-22 00:36 被阅读0次
- (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;
}


相关文章

网友评论

      本文标题:32.给图片添加水印

      本文链接:https://www.haomeiwen.com/subject/rtzfmktx.html