阅读 138

iOS-给图片添加水印效果的方法并保存图片到指定路径

iOS-给图片添加水印效果的方法并保存图片到指定路径

这里介绍两种方法:


方法一:添加文字水印,这里说的文字水印是指将开发者定义的文字直接添加到图片上,代码如下:


UIImage *img = [UIImage imageNamed:@"3G_alert.png"];  //需要加水印的图片

    CGSize size = self.view.frame.size; //设置上下文(画布)大小

    UIGraphicsBeginImageContext(size); //创建一个基于位图的上下文(context),并将其设置为当前上下文

    CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取当前上下文

    NSString *title = @"舵手网络";  //需要添加的水印文字

    CGContextTranslateCTM(contextRef, 0, self.view.bounds.size.height);  //画布的高度

    CGContextScaleCTM(contextRef, 1.0, -1.0);  //画布翻转

    CGContextDrawImage(contextRef, self.view.frame, [img CGImage]);  //在上下文种画当前图片

 

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

    CGContextTranslateCTM(contextRef, 0, self.view.bounds.size.height);

    CGContextScaleCTM(contextRef, 1.0, -1.0);

    UIFont *font = [UIFont boldSystemFontOfSize:40];

    [title drawInRect:CGRectMake(100, 400, 200, 80) withFont:font];

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

    UIGraphicsEndImageContext(); //移除栈顶的基于当前位图的图形上下文。

    NSArray *savePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *p = [savePath objectAtIndex:0];

    NSLog(@"%@",p);

    NSString *dataFilePath = [p stringByAppendingPathComponent:@"1.png"];

 

    NSData *imageData = UIImagePNGRepresentation(res);

    [imageData writeToFile:dataFilePath atomically:YES];

 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    [imageView setImage:res];

    [self.view addSubview:imageView];

 方法二:添加图片水印,顾名思义,就是两张图片,一张原图,一张水印图片,代码如下:


 UIImage *img = [UIImage imageNamed:@"demo.jpg"];  //需要加水印的图片

    UIImage *smallImg = [UIImage imageNamed:@"water.png"];

    CGSize size = self.view.frame.size; //设置上下文(画布)大小

    UIGraphicsBeginImageContext(size); //创建一个基于位图的上下文(context),并将其设置为当前上下文

    CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取当前上下文

    CGContextTranslateCTM(contextRef, 0, self.view.bounds.size.height);  //画布的高度

    CGContextScaleCTM(contextRef, 1.0, -1.0);  //画布翻转

 

    CGContextDrawImage(contextRef, self.view.frame, [img CGImage]);  //在上下文种画当前图片

    CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);

    UIImage *res = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); //移除栈顶的基于当前位图的图形上下文。

    NSArray *savePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *p = [savePath objectAtIndex:0];

    NSLog(@"%@",p);

    NSString *dataFilePath = [p stringByAppendingPathComponent:@"1.png"];

 

    NSData *imageData = UIImageJPEGRepresentation(res, 1.0);

    [imageData writeToFile:dataFilePath atomically:YES];

 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    [imageView setImage:res];

    [self.view addSubview:imageView];

 

————————————————

版权声明:本文为CSDN博主「jiufreeman」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/dcj3sjt126com/article/details/84445983


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐