首先如果我们想要在图片上随意的位置加水印,那么。水印应该是可以随手指移动而移动的,如下直接上代码:
- #import "MoveAbleImageView.h"
- @implementation MoveAbleImageView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [super touchesBegan:touches withEvent:event];
- }
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [super touchesEnded:touches withEvent:event];
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [super touchesMoved:touches withEvent:event];
- float dX = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;
- float dY = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
- //CGAffineTransformTranslate 是以transform为起点
- self.transform = CGAffineTransformTranslate(self.transform, dX, dY);
- }
注意!MoveAbleImageView 是继承于 UIImageView ,这个看个人需求都可以。 而且 MoveAbleImageView的 userInteractionEnabled属性要为YES,这样控件就可以跟随你手指移动而移动了。
接下来就要做合成图片了。我的想法有两种。一种是图片合成。别一种就是直接截图了。下面看代码
第一种:合成
- /**
- 加半透明水印
- @param useImage 需要加水印的图片
- @param addImage1 水印
- @returns 加好水印的图片
- */
- - (UIImage *)addImage:(UIImage *)useImage addMsakImage:(UIImage *)maskImage msakRect:(CGRect)rect
- {
- UIGraphicsBeginImageContext(useImage.size);
- [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];
- //四个参数为水印图片的位置
- [maskImage drawInRect:rect];
- UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return resultingImage;
- }
第二种:截屏
- -(UIImage *)saveImage:(UIView *)view {
- CGRect mainRect = [[UIScreen mainScreen] bounds];
- UIGraphicsBeginImageContext(CGSizeMake(320, 200));
- CGContextRef context = UIGraphicsGetCurrentContext();
- // [[UIColor blackColor] set];
- CGContextFillRect(context, mainRect);
- [view.layer renderInContext:context];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
这样基本就可以做成加水印的效果了。
转:http://blog.youkuaiyun.com/yu413854285/article/details/36867905