KKEmoticonTool.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KKEmoticonTool.m
  3. // WWImageEdit
  4. //
  5. // Created by 邬维 on 2017/1/12.
  6. // Copyright © 2017年 kook. All rights reserved.
  7. //
  8. #import "KKEmoticonTool.h"
  9. #import "KKToolBarItem.h"
  10. #import "KKEmoticonView.h"
  11. @implementation KKEmoticonTool{
  12. //修改前的图片
  13. UIImage *_originalImage;
  14. //工作区
  15. UIView *_workingView;
  16. //底部表情栏
  17. UIScrollView *_menuScroll;
  18. }
  19. #pragma -mark KKImageToolProtocol
  20. +(NSString *)defaultTitle{
  21. return Localized(@"JX_ImageEditAddImage");
  22. }
  23. +(NSUInteger)orderNum{
  24. return KKToolIndexNumberFourth;
  25. }
  26. +(UIImage *)defaultIconImage{
  27. return [UIImage imageNamed:@"ToolEmoicon"];
  28. }
  29. #pragma mark- implementation
  30. - (void)setup
  31. {
  32. _originalImage = self.editor.imageView.image;
  33. [self.editor fixZoomScaleWithAnimated:YES];
  34. _menuScroll = [[UIScrollView alloc] initWithFrame:self.editor.menuView.frame];
  35. _menuScroll.backgroundColor = self.editor.menuView.backgroundColor;
  36. _menuScroll.showsHorizontalScrollIndicator = NO;
  37. [self.editor.view addSubview:_menuScroll];
  38. //editor.imageView.superview 中的 editor.imageView 相对于 editor.view 的 frame
  39. _workingView = [[UIView alloc] initWithFrame:[self.editor.view convertRect:self.editor.imageView.frame fromView:self.editor.imageView.superview]];
  40. _workingView.clipsToBounds = YES;
  41. [self.editor.view addSubview:_workingView];
  42. [self setEmoticonMenu];
  43. _menuScroll.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_menuScroll.top);
  44. [UIView animateWithDuration:kImageToolAnimationDuration
  45. animations:^{
  46. _menuScroll.transform = CGAffineTransformIdentity;
  47. }];
  48. }
  49. - (void)cleanup
  50. {
  51. [self.editor resetZoomScaleWithAnimated:YES];
  52. [_workingView removeFromSuperview];
  53. [UIView animateWithDuration:kImageToolAnimationDuration
  54. animations:^{
  55. _menuScroll.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_menuScroll.top);
  56. }
  57. completion:^(BOOL finished) {
  58. [_menuScroll removeFromSuperview];
  59. }];
  60. }
  61. - (void)executeWithCompletionBlock:(void (^)(UIImage *, NSError *, NSDictionary *))completionBlock
  62. {
  63. [KKEmoticonView setActiveEmoticonView:nil];
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. UIImage *image = [self buildImage:_originalImage];
  66. completionBlock(image, nil, nil);
  67. });
  68. }
  69. #pragma mark-
  70. - (void)setEmoticonMenu
  71. {
  72. CGFloat W = 70;
  73. CGFloat H = _menuScroll.height;
  74. CGFloat x = 0;
  75. NSArray *list = @[@"banzai_search",@"1001",@"1002",@"1003",@"1004",@"1005",@"1006",@"1007",@"1008",@"1009",@"1010"];
  76. for(NSString *imgNmae in list){
  77. UIImage *image = [UIImage imageNamed:imgNmae];
  78. if(image){
  79. KKToolBarItem *view = [[KKToolBarItem alloc] initWithFrame:CGRectMake(x, 0, W, H) target:self action:@selector(tappedEmoticonPanel:) toolInfo:nil];
  80. view.iconView.image = image;
  81. [_menuScroll addSubview:view];
  82. x += W;
  83. }
  84. }
  85. _menuScroll.contentSize = CGSizeMake(MAX(x, _menuScroll.frame.size.width+1), 0);
  86. }
  87. - (void)tappedEmoticonPanel:(UITapGestureRecognizer*)sender
  88. {
  89. KKToolBarItem *kkview = (KKToolBarItem *)sender.view;
  90. KKEmoticonView *view = [[KKEmoticonView alloc] initWithImage:kkview.iconView.image tool:self];
  91. // CGFloat ratio = MIN( (0.5 * _workingView.width) / view.width, (0.5 * _workingView.height) / view.height);
  92. // [view setScale:ratio];
  93. view.center = CGPointMake(_workingView.width/2, _workingView.height/2);
  94. [_workingView addSubview:view];
  95. [KKEmoticonView setActiveEmoticonView:view];
  96. }
  97. //截屏
  98. - (UIImage*)buildImage:(UIImage*)image
  99. {
  100. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  101. [image drawAtPoint:CGPointZero];
  102. //缩放比例
  103. CGFloat scale = image.size.width / _workingView.width;
  104. CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
  105. [_workingView.layer renderInContext:UIGraphicsGetCurrentContext()];
  106. UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
  107. UIGraphicsEndImageContext();
  108. return tmp;
  109. }
  110. @end