zoukankan      html  css  js  c++  java
  • 画图

    感谢分享

    贝塞尔曲线,听着挺牛气一词,不过下面我们在做画图板的时候就用到贝塞尔绘直线,没用到绘制曲线的功能。如果会点PS的小伙伴会对贝塞尔曲线有更直观的理解。这篇文章的重点不在于如何用使用贝塞尔曲线,而是利用贝塞尔划线的功能来封装一个画图板。

    画图板的截图如下,上面的白板就是我们的画图板,是自己封装好的一个UIView,下面会详细的介绍如何封装这个画图板,下面的控件用来控制我们画图板的属性以及Undo,Redo和保存功能。点击保存时会把绘制的图片保存到手机的相册中。下面是具体的实现方案。

    一、 封装画图板

    其实上面的白板就是一继承于UiView的一个子类,我们可以在这个子类中添加我们画图板相应的属性和方法,然后实例化成对象添加到 ViewController中,当然为了省事添加白板的时候是通过storyboard来完成的,读者也可以自己实例化然后手动的添加到相应的 ViewController中。

    1. 封装白板的第一步是新建一个UIView的子类MyView,然后添加相应的属性和方法。MyView.h中的代码如下,代码具体意思请参照注释:

    1. #import  
    2.  
    3. @interface MyView : UIView 
    4. //用来设置线条的颜色 
    5. @property (nonatomic, strong) UIColor *color; 
    6. //用来设置线条的宽度 
    7. @property (nonatomic, assign) CGFloat lineWidth; 
    8. //用来记录已有线条 
    9. @property (nonatomic, strong) NSMutableArray *allLine; 
    10.  
    11. //初始化相关参数 
    12. -(void)initMyView; 
    13. //unDo操作 
    14. -(void)backImage; 
    15. //reDo操作 
    16. -(void)forwardImage; 
    17.  
    18. @end 

    2. 上面的代码是对外的接口,有些属性我们是写在MyView.m的延展中以实现私有的目的,MyView延展部分如下:

    1. @interface MyView() 
    2. //声明贝塞尔曲线 
    3. @property(nonatomic, strong) UIBezierPath *bezier; 
    4. //存储Undo出来的线条 
    5. @property(nonatomic, strong) NSMutableArray *cancleArray; 
    6. @end 

    3. 下面的代码就是实现部分的代码了,会根据不同功能给出相应的说明。

    (1) 初始化我们的白板,给线条指定默认颜色和宽度并且给相应的变量分配内存空间,初始化代码如下:

    1. //进行一些初始化工作 
    2. -(void)initMyView 
    3.     self.color = [UIColor redColor]; 
    4.     self.lineWidth = 1; 
    5.     self.allLine = [NSMutableArray arrayWithCapacity:50]; 
    6.     self.cancleArray = [NSMutableArray arrayWithCapacity:50]; 

    (2) Undo功能的封装,相当于两个栈,把显示的线条出栈,进入为不显示的线条栈中,每执行一次此操作显示线条栈中的元素会少一条而不显示线条栈中会多一条,大致就这个意思吧,代码如下:

    1. //UnDo操作 
    2. -(void)backImage 
    3.     if (self.allLine.count > 0) 
    4.     { 
    5.         int index = self.allLine.count - 1; 
    6.          
    7.         [self.cancleArray addObject:self.allLine[index]]; 
    8.          
    9.         [self.allLine removeObjectAtIndex:index]; 
    10.          
    11.         [self setNeedsDisplay 
    12.          ]; 
    13.     } 

    (3) Redo操作和Undo操作相反,从未显示栈中取出元素放入显示的栈中,代码中的栈我们是用数组来表示的,代码如下:

    1. //ReDo操作 
    2. -(void)forwardImage 
    3.     if (self.cancleArray.count > 0) 
    4.     { 
    5.         int index = self.cancleArray.count - 1; 
    6.          
    7.         [self.allLine addObject:self.cancleArray[index]]; 
    8.      
    9.         [self.cancleArray removeObjectAtIndex:index]; 
    10.          
    11.         [self setNeedsDisplay]; 
    12.     } 

    (4) 当开始触摸时我们新建一个BezierPath,把触摸起点设置成BezierPath的起点,并把将要画出的线条以及线条对应的属性封装成字典添加到显示栈中,代码如下

    1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    2.     //新建贝塞斯曲线 
    3.     self.bezier = [UIBezierPath bezierPath]; 
    4.      
    5.     //获取触摸的点 
    6.     UITouch *myTouche = [touches anyObject]; 
    7.     CGPoint point = [myTouche locationInView:self]; 
    8.      
    9.     //把刚触摸的点设置为bezier的起点 
    10.     [self.bezier moveToPoint:point]; 
    11.      
    12.     //把每条线存入字典中 
    13.     NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithCapacity:3]; 
    14.     [tempDic setObject:self.color forKey:@"color"]; 
    15.     [tempDic setObject:[NSNumber numberWithFloat:self.lineWidth] forKey:@"lineWidth"]; 
    16.     [tempDic setObject:self.bezier forKey:@"line"]; 
    17.      
    18.     //把线加入数组中 
    19.     [self.allLine addObject:tempDic]; 
    20.  

    (5) 当移动也就是划线的时候把点存储到BezierPath中,代码如下

    1. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    2.     UITouch *myTouche = [touches anyObject]; 
    3.     CGPoint point = [myTouche locationInView:self]; 
    4.      
    5.     [self.bezier addLineToPoint:point]; 
    6.      
    7.     //重绘界面 
    8.     [self setNeedsDisplay]; 
    9.      

    (6) 画出线条

    1. // Only override drawRect: if you perform custom drawing. 
    2. // An empty implementation adversely affects performance during animation. 
    3. - (void)drawRect:(CGRect)rect 
    4.     //对之前的线的一个重绘过程 
    5.     for (int i = 0; i < self.allLine.count; i ++) 
    6.     { 
    7.         NSDictionary *tempDic = self.allLine[i]; 
    8.         UIColor *color = tempDic[@"color"]; 
    9.         CGFloat width = [tempDic[@"lineWidth"] floatValue]; 
    10.         UIBezierPath *path = tempDic[@"line"]; 
    11.          
    12.         [color setStroke]; 
    13.         [path setLineWidth:width]; 
    14.         [path stroke]; 
    15.     } 
    16.  

     

    二、 画图板的使用

    上面是封装画图板要用到的全部代码,下面的代码就是如何在ViewController中使用我们的画图板了,如何实例化控件,以及控件的初始化,注册回调等在这就不做赘述了,下面给出了主要控件的回调方法。

    1. 通过Slider来调节线条的宽度

    1. //通过slider来设置线条的宽度 
    2. - (IBAction)sliderChange:(id)sender 
    3.     self.myView.lineWidth = self.mySlider.value; 

    2. 通过SegmentControl来设置线条的颜色

    1. /通过segmentControl来设置线条的颜色 
    2. - (IBAction)tapSegment:(id)sender { 
    3.      
    4.     switch (self.mySegment.selectedSegmentIndex) { 
    5.         case 0: 
    6.             self.myView.color = [UIColor redColor]; 
    7.             break; 
    8.         case 1: 
    9.             self.myView.color = [UIColor blackColor]; 
    10.             break; 
    11.         case 2: 
    12.             self.myView.color = [UIColor greenColor]; 
    13.             break; 
    14.              
    15.         default: 
    16.             break; 
    17.     } 
    18.      

    3. undo和redo操作

    1. //Undo 
    2. - (IBAction)tapBack:(id)sender { 
    3.     [self.myView backImage]; 
    4.  
    5.  
    6. //Redo操作 
    7. - (IBAction)tapGo:(id)sender { 
    8.     [self.myView forwardImage]; 

    4. 保存操作,也许下面的保存操作在处理方式上略显笨拙,如有更好的解决方案请留言。 保存的时候我是先截了个屏,然后把白板进行切割,把切割后图片存入到相册中,代码如下:

    1. //把画的图保存到相册 
    2. - (IBAction)tapSave:(id)sender { 
    3.     //截屏 
    4.     UIGraphicsBeginImageContext(self.view.bounds.size); 
    5.     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    6.     UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext(); 
    7.     UIGraphicsEndImageContext(); 
    8.      
    9.      
    10.     //截取画图版部分 
    11.     CGImageRef sourceImageRef = [uiImage CGImage]; 
    12.     CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, CGRectMake(36, 6, 249, 352)); 
    13.     UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 
    14.      
    15.     //把截的屏保存到相册 
    16.     UIImageWriteToSavedPhotosAlbum(newImage , nil, nil, nil); 
    17.      
    18.     //给个保存成功的反馈 
    19.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"存储照片成功" 
    20.                                                     message:@"您已将照片存储于图片库中,打开照片程序即可查看。" 
    21.                                                    delegate:self 
    22.                                           cancelButtonTitle:@"OK" 
    23.                                           otherButtonTitles:nil]; 
    24.     [alert show]; 
    25.  

    以上就是本画图板的主要代码了,有不足之处还望批评指正。转载请注明出处。在本文结束时在来几张截图吧(demo下载地址:http://www.pgyer.com/LTQ8)

  • 相关阅读:
    img在src无效或者是空的时候出现边框
    console.log((!+[]+[]+![]).length)
    JQuery与zTree记录
    axis2框架实现webservice
    jsp页面常用代码
    使用MyEclipse实现通过数据库中表和hibernate生成实体类和映射配置文件
    PL/SQL Developer工具的使用简介
    有关静态代码块、代码块、构造方法执行顺序
    完全平方数的个数
    队列Q(排序方式)
  • 原文地址:https://www.cnblogs.com/isItOk/p/5618649.html
Copyright © 2011-2022 走看看