图片任意位置添加水印

本文介绍了一种在iOS应用中实现图片加水印的方法。通过自定义UIImageView子类MoveAbleImageView,使得水印能够随手指移动。文章提供了两种生成带水印图片的方式:图片合成与屏幕截图,并附上了具体的实现代码。

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

首先如果我们想要在图片上随意的位置加水印,那么。水印应该是可以随手指移动而移动的,如下直接上代码:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "MoveAbleImageView.h"  
  2.   
  3. @implementation MoveAbleImageView  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self) {  
  9.         // Initialization code  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  15. {  
  16.     [super touchesBegan:touches withEvent:event];  
  17. }  
  18.   
  19.   
  20. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  21. {  
  22.     [super touchesEnded:touches withEvent:event];  
  23. }  
  24.   
  25.   
  26. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  27. {  
  28.     [super touchesMoved:touches withEvent:event];  
  29.       
  30.     float dX = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;  
  31.     float dY = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;  
  32.     //CGAffineTransformTranslate 是以transform为起点  
  33.     self.transform = CGAffineTransformTranslate(self.transform, dX, dY);  
  34. }  

注意!MoveAbleImageView 是继承于 UIImageView ,这个看个人需求都可以。 而且 MoveAbleImageView的 userInteractionEnabled属性要为YES,这样控件就可以跟随你手指移动而移动了。

接下来就要做合成图片了。我的想法有两种。一种是图片合成。别一种就是直接截图了。下面看代码

第一种:合成

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.     加半透明水印 
  3.     @param useImage 需要加水印的图片 
  4.     @param addImage1 水印 
  5.     @returns 加好水印的图片 
  6.  */  
  7. - (UIImage *)addImage:(UIImage *)useImage addMsakImage:(UIImage *)maskImage msakRect:(CGRect)rect  
  8. {  
  9.     UIGraphicsBeginImageContext(useImage.size);  
  10.     [useImage drawInRect:CGRectMake(00, useImage.size.width, useImage.size.height)];  
  11.       
  12.     //四个参数为水印图片的位置  
  13.     [maskImage drawInRect:rect];  
  14.     UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
  15.     UIGraphicsEndImageContext();  
  16.     return resultingImage;  
  17. }  
maskImage就是水印图。rect为水印图位置


第二种:截屏

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. -(UIImage *)saveImage:(UIView *)view {  
  2.     CGRect mainRect = [[UIScreen mainScreen] bounds];  
  3.       
  4.     UIGraphicsBeginImageContext(CGSizeMake(320200));  
  5.     CGContextRef context = UIGraphicsGetCurrentContext();  
  6. //    [[UIColor blackColor] set];  
  7.       
  8.     CGContextFillRect(context, mainRect);  
  9.     [view.layer renderInContext:context];  
  10.       
  11.     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
  12.       
  13.     UIGraphicsEndImageContext();  
  14.       
  15.     return newImage;  
  16. }  

这样基本就可以做成加水印的效果了。


转:http://blog.youkuaiyun.com/yu413854285/article/details/36867905

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值