Using only one tool from CLImageEditor

2.3k Views Asked by At

I am working on adding text on image. I found that CLImageEditor tool is very good. but i only want to use add text tool with my own theme. so can i use that tool in my app. or any other tool that i can use in my app.

4

There are 4 best solutions below

0
On BEST ANSWER

This is possible by tweeking the existing code as below:

    //First disable all the tools like below but the one you need.
          var tool = editor.toolInfo.subToolInfoWithToolName("CLRotateTool", recursive: false)
          tool.available = false

    //replace below 4 functions in CLImageEditorView Controller

    - (void)setMenuView
    {
        CGFloat x = 0;
        CGFloat W = 70;
        CGFloat H = _menuView.height;

        int toolCount = 0;
        CGFloat padding = 0;
        for(CLImageToolInfo *info in self.toolInfo.sortedSubtools){
            if(info.available){
                toolCount++;
            }
        }

        CGFloat diff = _menuView.frame.size.width - toolCount * W;
        if (0<diff && diff<2*W) {
            padding = diff/(toolCount+1);
        }

        for(CLImageToolInfo *info in self.toolInfo.sortedSubtools){
            if(!info.available){
                continue;
            }

            CLToolbarMenuItem *view = [CLImageEditorTheme menuItemWithFrame:CGRectMake(x+padding, 0, W, H) target:self action:@selector(tappedMenuView:) toolInfo:info];
            [_menuView addSubview:view];
            x += W+padding;
            [self tappedMenuView:view];

        }
        _menuView.contentSize = CGSizeMake(MAX(x, _menuView.frame.size.width+1), 0);
    }

    - (IBAction)pushedCancelBtn:(id)sender
    {
        _imageView.image = _originalImage;
        [self resetImageViewFrame];
        self.currentTool = nil;
        [self  pushedCloseBtn:nil];

    }

- (IBAction)pushedDoneBtn:(id)sender
{
    self.view.userInteractionEnabled = NO;

    [self.currentTool executeWithCompletionBlock:^(UIImage *image, NSError *error, NSDictionary *userInfo) {
        if(error){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else if(image){
            _originalImage = image;
            _imageView.image = image;

            [self resetImageViewFrame];
            self.currentTool = nil;
        }
        self.view.userInteractionEnabled = YES;
        [self pushedFinishBtn:nil];
    }];
}


    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.title = self.toolInfo.title;
        self.view.clipsToBounds = YES;
        self.view.backgroundColor = self.theme.backgroundColor;
        self.navigationController.view.backgroundColor = self.view.backgroundColor;

        if([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]){
            self.automaticallyAdjustsScrollViewInsets = NO;
        }

        if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]){
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }

        [self initNavigationBar];
        [self initMenuScrollView];
        [self initImageScrollView];


        if(_imageView==nil){
            _imageView = [UIImageView new];
            [_scrollView addSubview:_imageView];
            [self refreshImageView];
        }
        [self setMenuView];

    }
0
On

No, you cant directly open a submenu. May be raise an issue to them at https://github.com/yackle/CLImageEditor/issues

If you want to show only few options or you want to customize item location you can do this way:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    CLImageEditor *editor = [[CLImageEditor alloc] initWithImage:image];
    editor.delegate = self;

    CLImageToolInfo *tool1 = [editor.toolInfo subToolInfoWithToolName:@"CLAdjustmentTool" recursive:NO];
    CLImageToolInfo *tool2 = [editor.toolInfo subToolInfoWithToolName:@"CLBlurTool" recursive:NO];
    CLImageToolInfo *tool3 = [editor.toolInfo subToolInfoWithToolName:@"CLRotateTool" recursive:NO];
    CLImageToolInfo *tool4 = [editor.toolInfo subToolInfoWithToolName:@"CLToneCurveTool" recursive:NO];

    tool1.available = NO;
    tool2.available = NO;
    tool3.available = NO;
    tool4.available = NO;
    [picker pushViewController:editor animated:YES];
}
1
On

You could do this for every tool.

 CLImageToolInfo *tool = [editor.toolInfo subToolInfoWithToolName:@"CLToneCurveTool" recursive:NO];
    tool.available = NO;     // if available is set to NO, it is removed from the menu view.
0
On

You can not open submenu directly.

I created a remover function for removing the subtools. Swift 4.2

private func removeTools(tools: [String]) {
    tools.forEach {
      guard let imageEditor = self.imageEditor else { return }
      let tool = imageEditor.toolInfo.subToolInfo(withToolName: $0, recursive: false)
      tool?.available = false
    }
  }

You can call like this;

removeTools(tools: ["CLFilterTool", "CLEffectTool"])