KKDrawTool.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // KKDrawTool.m
  3. // WWImageEdit
  4. //
  5. // Created by 邬维 on 2017/1/11.
  6. // Copyright © 2017年 kook. All rights reserved.
  7. //
  8. #import "KKDrawTool.h"
  9. @implementation KKDrawTool{
  10. UIImageView *_drawingView; //画线view
  11. CGSize _originalImageSize; //初始大小
  12. CGPoint _prevDraggingPosition; //拖动的起点
  13. UIView *_menuView; //菜单栏
  14. UISlider *_colorSlider;
  15. UISlider *_widthSlider;
  16. UIView *_strokePreview;
  17. UIView *_strokePreviewBackground;
  18. UIImageView *_eraserIcon; //橡皮擦
  19. }
  20. #pragma -mark KKImageToolProtocol
  21. + (UIImage*)defaultIconImage{
  22. return [UIImage imageNamed:@"ToolDraw"];
  23. }
  24. + (NSString*)defaultTitle{
  25. return Localized(@"JX_ImageEditDraw");
  26. }
  27. + (NSUInteger)orderNum{
  28. return KKToolIndexNumberSecond;
  29. }
  30. #pragma mark- implementation
  31. - (void)setup
  32. {
  33. _originalImageSize = self.editor.imageView.image.size;
  34. _drawingView = [[UIImageView alloc] initWithFrame:self.editor.imageView.bounds];
  35. UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)];
  36. panGesture.maximumNumberOfTouches = 1;
  37. _drawingView.userInteractionEnabled = YES;
  38. [_drawingView addGestureRecognizer:panGesture];
  39. [self.editor.imageView addSubview:_drawingView];
  40. self.editor.imageView.userInteractionEnabled = YES;
  41. self.editor.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;
  42. self.editor.scrollView.panGestureRecognizer.delaysTouchesBegan = NO;
  43. self.editor.scrollView.pinchGestureRecognizer.delaysTouchesBegan = NO;
  44. _menuView = [[UIView alloc] initWithFrame:CGRectMake(self.editor.menuView.frame.origin.x, self.editor.view.height - 80, self.editor.menuView.frame.size.width, 80)];
  45. _menuView.backgroundColor = self.editor.menuView.backgroundColor;
  46. [self.editor.view addSubview:_menuView];
  47. [self setMenu];
  48. _menuView.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_menuView.top);
  49. [UIView animateWithDuration:kImageToolAnimationDuration
  50. animations:^{
  51. _menuView.transform = CGAffineTransformIdentity;
  52. }];
  53. }
  54. - (void)cleanup
  55. {
  56. [_drawingView removeFromSuperview];
  57. self.editor.imageView.userInteractionEnabled = NO;
  58. self.editor.scrollView.panGestureRecognizer.minimumNumberOfTouches = 1;
  59. [UIView animateWithDuration:kImageToolAnimationDuration
  60. animations:^{
  61. _menuView.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_menuView.top);
  62. }
  63. completion:^(BOOL finished) {
  64. [_menuView removeFromSuperview];
  65. }];
  66. }
  67. - (void)executeWithCompletionBlock:(void (^)(UIImage *, NSError *, NSDictionary *))completionBlock
  68. {
  69. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. UIImage *image = [self buildImage];
  72. completionBlock(image, nil, nil);
  73. });
  74. // });
  75. }
  76. #pragma mark- other
  77. - (UISlider*)defaultSliderWithWidth:(CGFloat)width
  78. {
  79. UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, width, 34)];
  80. [slider setMaximumTrackImage:[UIImage new] forState:UIControlStateNormal];
  81. [slider setMinimumTrackImage:[UIImage new] forState:UIControlStateNormal];
  82. [slider setThumbImage:[UIImage new] forState:UIControlStateNormal];
  83. slider.thumbTintColor = [UIColor whiteColor];
  84. return slider;
  85. }
  86. - (UIImage*)colorSliderBackground
  87. {
  88. CGSize size = _colorSlider.frame.size;
  89. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
  90. CGContextRef context = UIGraphicsGetCurrentContext();
  91. CGRect frame = CGRectMake(5, (size.height-10)/2, size.width-10, 5);
  92. CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:5].CGPath;
  93. CGContextAddPath(context, path);
  94. CGContextClip(context);
  95. CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
  96. CGFloat components[] = {
  97. 0.0f, 0.0f, 0.0f, 1.0f,
  98. 1.0f, 1.0f, 1.0f, 1.0f,
  99. 1.0f, 0.0f, 0.0f, 1.0f,
  100. 1.0f, 1.0f, 0.0f, 1.0f,
  101. 0.0f, 1.0f, 0.0f, 1.0f,
  102. 0.0f, 1.0f, 1.0f, 1.0f,
  103. 0.0f, 0.0f, 1.0f, 1.0f
  104. };
  105. size_t count = sizeof(components)/ (sizeof(CGFloat)* 4);
  106. CGFloat locations[] = {0.0f, 0.9/3.0, 1/3.0, 1.5/3.0, 2/3.0, 2.5/3.0, 1.0};
  107. CGPoint startPoint = CGPointMake(5, 0);
  108. CGPoint endPoint = CGPointMake(size.width-5, 0);
  109. CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, components, locations, count);
  110. CGContextDrawLinearGradient(context, gradientRef, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  111. UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
  112. CGGradientRelease(gradientRef);
  113. CGColorSpaceRelease(colorSpaceRef);
  114. UIGraphicsEndImageContext();
  115. return tmp;
  116. }
  117. - (UIImage*)widthSliderBackground
  118. {
  119. CGSize size = _widthSlider.frame.size;
  120. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
  121. CGContextRef context = UIGraphicsGetCurrentContext();
  122. UIColor *color = [[[KKImageEditorTheme theme] toolbarTextColor] colorWithAlphaComponent:0.5];
  123. CGFloat strRadius = 1;
  124. CGFloat endRadius = size.height/2 * 0.6;
  125. CGPoint strPoint = CGPointMake(strRadius + 5, size.height/2 - 2);
  126. CGPoint endPoint = CGPointMake(size.width-endRadius - 1, strPoint.y);
  127. CGMutablePathRef path = CGPathCreateMutable();
  128. CGPathAddArc(path, NULL, strPoint.x, strPoint.y, strRadius, -M_PI/2, M_PI-M_PI/2, YES);
  129. CGPathAddLineToPoint(path, NULL, endPoint.x, endPoint.y + endRadius);
  130. CGPathAddArc(path, NULL, endPoint.x, endPoint.y, endRadius, M_PI/2, M_PI+M_PI/2, YES);
  131. CGPathAddLineToPoint(path, NULL, strPoint.x, strPoint.y - strRadius);
  132. CGPathCloseSubpath(path);
  133. CGContextAddPath(context, path);
  134. CGContextSetFillColorWithColor(context, color.CGColor);
  135. CGContextFillPath(context);
  136. UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
  137. CGPathRelease(path);
  138. UIGraphicsEndImageContext();
  139. return tmp;
  140. }
  141. - (UIColor*)colorForValue:(CGFloat)value
  142. {
  143. if(value<1/3.0){
  144. return [UIColor colorWithWhite:value/0.3 alpha:1];
  145. }
  146. return [UIColor colorWithHue:((value-1/3.0)/0.7)*2/3.0 saturation:1 brightness:1 alpha:1];
  147. }
  148. - (void)setMenu
  149. {
  150. CGFloat W = 70;
  151. _colorSlider = [self defaultSliderWithWidth:_menuView.width - W - 20];
  152. _colorSlider.left = 10;
  153. _colorSlider.top = 5;
  154. [_colorSlider addTarget:self action:@selector(colorSliderDidChange:) forControlEvents:UIControlEventValueChanged];
  155. _colorSlider.backgroundColor = [UIColor colorWithPatternImage:[self colorSliderBackground]];
  156. _colorSlider.value = 0;
  157. [_menuView addSubview:_colorSlider];
  158. _widthSlider = [self defaultSliderWithWidth:_colorSlider.width];
  159. _widthSlider.left = 10;
  160. _widthSlider.top = _colorSlider.bottom + 5;
  161. [_widthSlider addTarget:self action:@selector(widthSliderDidChange:) forControlEvents:UIControlEventValueChanged];
  162. _widthSlider.value = 0.1;
  163. _widthSlider.backgroundColor = [UIColor colorWithPatternImage:[self widthSliderBackground]];
  164. [_menuView addSubview:_widthSlider];
  165. _strokePreview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W - 5, W - 5)];
  166. _strokePreview.layer.cornerRadius = _strokePreview.height/2;
  167. _strokePreview.layer.borderWidth = 1;
  168. _strokePreview.layer.borderColor = [[[KKImageEditorTheme theme] toolbarTextColor] CGColor];
  169. _strokePreview.center = CGPointMake(_menuView.width-W/2, _menuView.height/2);
  170. [_menuView addSubview:_strokePreview];
  171. _strokePreviewBackground = [[UIView alloc] initWithFrame:_strokePreview.frame];
  172. _strokePreviewBackground.layer.cornerRadius = _strokePreviewBackground.height/2;
  173. _strokePreviewBackground.alpha = 0.3;
  174. [_strokePreviewBackground addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(strokePreviewDidTap:)]];
  175. [_menuView insertSubview:_strokePreviewBackground aboveSubview:_strokePreview];
  176. _eraserIcon = [[UIImageView alloc] initWithFrame:_strokePreview.frame];
  177. _eraserIcon.image = [UIImage imageNamed:@"eraser"];
  178. _eraserIcon.hidden = YES;
  179. [_menuView addSubview:_eraserIcon];
  180. [self colorSliderDidChange:_colorSlider];
  181. [self widthSliderDidChange:_widthSlider];
  182. _menuView.clipsToBounds = NO;
  183. }
  184. - (void)colorSliderDidChange:(UISlider*)sender
  185. {
  186. if(_eraserIcon.hidden){
  187. _strokePreview.backgroundColor = [self colorForValue:_colorSlider.value];
  188. _strokePreviewBackground.backgroundColor = _strokePreview.backgroundColor;
  189. _colorSlider.thumbTintColor = _strokePreview.backgroundColor;
  190. }
  191. }
  192. - (void)widthSliderDidChange:(UISlider*)sender
  193. {
  194. CGFloat scale = MAX(0.05, _widthSlider.value);
  195. _strokePreview.transform = CGAffineTransformMakeScale(scale, scale);
  196. _strokePreview.layer.borderWidth = 2/scale;
  197. }
  198. - (void)strokePreviewDidTap:(UITapGestureRecognizer*)sender
  199. {
  200. _eraserIcon.hidden = !_eraserIcon.hidden;
  201. if(_eraserIcon.hidden){
  202. [self colorSliderDidChange:_colorSlider];
  203. }
  204. else{
  205. _strokePreview.backgroundColor = [[KKImageEditorTheme theme] toolbarTextColor];
  206. _strokePreviewBackground.backgroundColor = _strokePreview.backgroundColor;
  207. }
  208. }
  209. //监听手指移动
  210. - (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender
  211. {
  212. CGPoint currentDraggingPosition = [sender locationInView:_drawingView];
  213. if(sender.state == UIGestureRecognizerStateBegan){
  214. _prevDraggingPosition = currentDraggingPosition;
  215. }
  216. if(sender.state != UIGestureRecognizerStateEnded){
  217. [self drawLine:_prevDraggingPosition to:currentDraggingPosition];
  218. }
  219. _prevDraggingPosition = currentDraggingPosition;
  220. }
  221. //画线
  222. -(void)drawLine:(CGPoint)from to:(CGPoint)to
  223. {
  224. CGSize size = _drawingView.frame.size;
  225. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
  226. CGContextRef context = UIGraphicsGetCurrentContext();
  227. [_drawingView.image drawAtPoint:CGPointZero];
  228. CGFloat strokeWidth = MAX(1, _widthSlider.value * 65);
  229. UIColor *strokeColor = _strokePreview.backgroundColor;
  230. CGContextSetLineWidth(context, strokeWidth);
  231. CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
  232. CGContextSetLineCap(context, kCGLineCapRound);
  233. if(!_eraserIcon.hidden){
  234. CGContextSetBlendMode(context, kCGBlendModeClear);
  235. }
  236. CGContextMoveToPoint(context, from.x, from.y);
  237. CGContextAddLineToPoint(context, to.x, to.y);
  238. CGContextStrokePath(context);
  239. _drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
  240. UIGraphicsEndImageContext();
  241. }
  242. //生成修改后的图片
  243. - (UIImage*)buildImage
  244. {
  245. UIGraphicsBeginImageContextWithOptions(_originalImageSize, NO, self.editor.imageView.image.scale);
  246. [self.editor.imageView.image drawAtPoint:CGPointZero];
  247. [_drawingView.image drawInRect:CGRectMake(0, 0, _originalImageSize.width, _originalImageSize.height)];
  248. UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
  249. UIGraphicsEndImageContext();
  250. return tmp;
  251. }
  252. @end