iOS 图片编辑——涂鸦——在图片上添加文字

前面几节讲了图片上画线

这一节 我们简单讲一讲 给图片上添加文字的方法 我们继续使用上节的代码(代码下载地址见上节末尾   http://blog.csdn.net/lwjok2007/article/details/50887396

首先我们将添加文字的View单独抽象出现实现

创建一个类 继承自UIView

起名: AddText

《iOS 图片编辑——涂鸦——在图片上添加文字》

定义一个协议 当输入完成之后 将内容返回给ViewController

#import <UIKit/UIKit.h>


@protocol EditTextDelegate <NSObject>

-(void)ADDTextWithText:(NSString *)TEXT;

@end


@interface AddText : UIView


@property(nonatomic,strong)id<EditTextDelegate>delegate;

@end

实现AddText

#import "AddText.h"

@implementation AddText


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        [self initUserInterface];
    }
    return self;
}

//添加控件
- (void)initUserInterface{
    //取消按钮
    UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCancel.frame = CGRectMake(5, 5, 60, 30);
    [btnCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(cancelAct) forControlEvents:UIControlEventTouchUpInside];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:btnCancel];
    
    
    //确认按钮
    UIButton *btnSubmit = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSubmit.frame = CGRectMake(self.frame.size.width-65, 5, 60, 30);
    [btnSubmit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnSubmit addTarget:self action:@selector(submitAct) forControlEvents:UIControlEventTouchUpInside];
    [btnSubmit setTitle:@"确定" forState:UIControlStateNormal];
    [self addSubview:btnSubmit];
    
    //分割线
    UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, self.frame.size.width, 1)];
    labe.backgroundColor = [UIColor brownColor];
    [self addSubview:labe];
    
    //输入框
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 45,self.frame.size.width , self.frame.size.height-120)];
    textView.tag = 600;
    textView.font = [UIFont systemFontOfSize:20];
    textView.backgroundColor = [UIColor cyanColor];
    [self addSubview:textView];
}

- (void)cancelAct{
    
    [self removeFromSuperview];
}

- (void)submitAct{
    
    UITextView *textView = (UITextView *)[self viewWithTag:600];
    [self.delegate ADDTextWithText:textView.text];
    [self removeFromSuperview];
}

接下来 我们给ViewController添加一个按钮 点击按钮之后会打开AddText界面

    
    UIButton *addTextBtn= [[UIButton alloc]initWithFrame:CGRectMake(screen_Width/2.0+60+10, 60, 120, 36)];
    [addTextBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addTextBtn setTitle:@"文字" forState:UIControlStateNormal];
    [addTextBtn addTarget:self action:@selector(addTextAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addTextBtn];

//添加文字按钮事件
- (void)addTextAct:(id)sender{
    
    AddText *editText = [[AddText alloc]initWithFrame:CGRectMake(60, 120, screen_Width-120, screen_Width*2/3)];
    editText.tag = 650;
    editText.delegate = self;
    [self.view addSubview:editText];
    
}

实现AddText的代理 方法获取到值之后显示到图片上

同时我们要求 文字的位置可以随意挪动。

同时 还有文字的放大 旋转等 大家自己研究一下

#pragma mark 增加文字代理方法
- (void)ADDTextWithText:(NSString *)TEXT
{
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 360)];
    textlabel.tag = 900;
    textlabel.lineBreakMode = 0;
    textlabel.font = [UIFont systemFontOfSize:25];
    textlabel.text = TEXT;
    [textlabel sizeToFit];
    textlabel.center = CGPointMake(screen_Width/2.0, screen_Height/2.0);
    textlabel.userInteractionEnabled = YES;
    [imageV addSubview:textlabel];
    //拖拽
    UIPanGestureRecognizer *panLabel = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(tapLabel:)];
    [textlabel addGestureRecognizer:panLabel];
//    //旋转
//    UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
//    [textlabel addGestureRecognizer:rota];
//    //缩放
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
//    [textlabel addGestureRecognizer:pinch];
    
//      [textlabel becomeFirstResponder];
}

//拖拽
- (void)tapLabel:(UIPanGestureRecognizer *)panLabel
{
    
    UILabel *textlabel = (UILabel *)[self.view viewWithTag:900];
    CGPoint point =  [panLabel  translationInView:textlabel];
    // NSLog(@"%f %f",point.x ,point.y);
    //改变中心点坐标(原来的中心点+偏移量=当前的中心点)
    
    panLabel.view.center = CGPointMake(panLabel.view. center.x+point.x, panLabel.view.center.y+point.y);

    
    //每次调用完之后,需要重置手势的偏移量,否则平移手势会自动累加偏移量
    //CGPointMake(0, 0)<==>CGPointZero
    [panLabel setTranslation:CGPointZero inView:textlabel];
    
}

好了 我们运行试试

《iOS 图片编辑——涂鸦——在图片上添加文字》

如果说看到了 弹出的输入框 那么就成功一半了。输入文字点击确认 图片上是不是多出了文字 拖动试试

如果拖动没有反应

接的设置一下imageV

    imageV.userInteractionEnabled=YES;

我们前面讲了那么多,图片上添加了各种东西 那么最后怎么保存图片了?

方法很简单 我们在 ViewController中添加一个按钮 当点击的时候 将修改过的图片生成 现实到一个新的imageview上

添加一个ImnageView

添加一个Button 添加点击事件

    imageV1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screen_Width, screen_Height/2)];
    [self.view addSubview:imageV1];

好了 我们就简单实现一下 更为复杂的功能我们下来再研究

    
    UIButton *saveImageBtn= [[UIButton alloc]initWithFrame:CGRectMake(screen_Width/2.0+60+10, 30, 120, 36)];
    [saveImageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [saveImageBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveImageBtn addTarget:self action:@selector(saveImageAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveImageBtn];
- (void)saveImageAct:(id)sender{
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(screen_Width, imageV.frame.size.height), YES, 1.0);
    [imageV.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    imageV1.image=uiImage;
    
}

好了  我们试试 看 图片已经可以生成了

代码我会上传到群空间 有兴趣可以去下载

demo: 【60315图片上添加文字FingerLine.zip】

苹果开发群 :414319235  欢迎加入,共同学习

    原文作者:lwjok2007
    原文地址: https://blog.csdn.net/lwjok2007/article/details/50896455
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞