iOS 添加多圖片加簽名

如效果圖,首先向相冊獲取圖片

 

給圖片添加多種手勢。

- (void)addGes {
//pan
//拖拽手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan]; git

//pinch
//捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
//添加旋轉
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
//長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}github

 

捏合手勢,還要記住圖片的狀態,復位數組

//捏合
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
//復位
pinch.scale = 1;
}post

 

旋轉的時候調用spa

- (void)rotation:(UIRotationGestureRecognizer *)rotation {
//旋轉圖片
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
//復位,只要想相對於上一次旋轉就復位
rotation.rotation = 0;
}代理

 

何時調用:長按的時候調用,並且只要手指不離開,拖動的時候會一直調用,手指擡起的時候也會調用orm

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
//設置透明度
[UIView animateWithDuration:0.25 animations:^{
self.imageView.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
self.imageView.alpha = 1;
} completion:^(BOOL finished) {
//對當前的view截屏
//1.開啓一個位圖上下文
/**
UIGraphicsBeginImageContext //模糊
---------------------
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)//原圖
opaque 透明度,不透明設爲YES;
scale 縮放因子,設0時系統自動設置縮放比例圖片清晰;設1.0時模糊
---------------------
*/
if (self.isHighDefinition) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(self.bounds.size);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];

//從上下文當中生成一張新的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//調用代理方法
if ([self.delegate respondsToSelector:@selector(handleView:newImage:)]) {
[self.delegate handleView:self newImage:newImage];
} blog

//移除當前view
[self removeFromSuperview];
//關閉上下文
UIGraphicsEndImageContext();
}];
}];
}
}繼承

 

拖動的時候調用圖片

- (void)pan:(UIPanGestureRecognizer *)pan {
CGPoint transP = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, transP.x, transP.y);
//復位
[pan setTranslation:CGPointZero inView:pan.view];
}

 

可以同時支持多個手勢

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

 

給圖片畫像

- (void)pan:(UIPanGestureRecognizer *)pan {
//獲取當前手勢所在點
CGPoint curP = [pan locationInView:self];

//畫線
if (pan.state == UIGestureRecognizerStateBegan) {
//建立路徑
YMBezierPath *path = [YMBezierPath bezierPath];
path.lineWidth = self.width;
self.path = path;
path.lineJoinStyle = kCGLineJoinRound;//線的樣式
path.lineCapStyle = kCGLineCapRound;//線頭的樣式
//當發現系統的類,沒有辦法知足咱們的要求時,繼承系統類,添加屬性知足本身的需求
//顏色必須得要在drawRect方法中進行繪製
path.lineColor = self.color;
//設置路徑的起點
[path moveToPoint:curP];

//保存路徑
[self.pathArr addObject:path];
} else if (
pan.state == UIGestureRecognizerStateChanged) {
//添加一根線到當前手指所在的點
[self.path addLineToPoint:curP];
//重繪
[self setNeedsDisplay];
}
}

繪製全部路徑

- (void)drawRect:(CGRect)rect {
for (YMBezierPath *path in self.pathArr) {
if ([path isKindOfClass:[UIImage class]]) {
UIImage *image = (UIImage *)path;
[image drawInRect:rect];
} else {
[path.lineColor set];
[path stroke];
}
}
}

 

清屏 

- (void)clear {
//清空數組,而後重繪
[self.pathArr removeAllObjects];
[self setNeedsDisplay];
}

 

 撤銷 

- (void)undo {
[self.pathArr removeLastObject];
[self setNeedsDisplay];
}

 

保存自定之後的圖片

- (IBAction)saveBtnClick {
[UIView animateWithDuration:0.25 animations:^{
self.drawView.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
self.drawView.alpha = 1;
} completion:^(BOOL finished) {
//對畫板作截屏
// UIGraphicsBeginImageContext(self.drawView.bounds.size);//模糊
UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 0.0);//原圖

//把View的layer的內容渲染到上下文當中
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.drawView.layer renderInContext:ctx];

//從上下文當中生成一張圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

//關閉上下文
UIGraphicsEndImageContext();

//把生成的圖片保存到系統相冊中
//保存完畢時調用的方法必須得是:image:didFinishSavingWithError:contextInfo:
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}];
}];
}

 

最後合成的圖片如圖:

 

項目Demo:https://github.com/AndrewLJJ/DrawingBoard

掘金:https://juejin.im/post/5d8acdd8e51d4577fc7b1be1

相關文章
相關標籤/搜索