画板案例

知识点:将View上的信息保存到相册

关于绘制图形、路线

 

******************************************

#import <UIKit/UIKit.h>

@interface FFFPaintView : UIView

@property (nonatomic,strong) UIColor *lineColor;

@property(nonatomic,assign) CGFloat lineWithed;

@property (nonatomic,copy) CGFloat(^lineWithBlock)();

//清屏

-(void)clearScreen;

//回退

-(void)recede;

//橡皮擦

-(void)eraser;

@end

******************************************

#import "FFFPaintView.h"

@interface FFFBezierPath : UIBezierPath

@property (nonatomic,strong) UIColor *lineColor;

@end

@implementationFFFBezierPath

@end

 

@interface FFFPaintView ()

@property (nonatomic,strong) NSMutableArray *paths;

@end

@implementationFFFPaintView

 

-(NSMutableArray *)paths{

 

    if(_paths==nil){

    

       _paths = [NSMutableArrayarray];

    }

    return _paths;

}

//清屏

-(void)clearScreen{

    

//    注意这里有时候移除全部的时候会出错,所以为了防止,建议 == nil,即可,然后在重绘

    [self.paths removeAllObjects];

    self.paths = nil;

    //   重绘

    [self setNeedsDisplay];

}

//回退

-(void)recede{

    [self.paths removeLastObject];

    //   重绘

    [self setNeedsDisplay];

}

//橡皮擦

-(void)eraser{

    self.lineColor = self.backgroundColor;

    //   重绘

    [self setNeedsDisplay];

}

 

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

//   获取触摸的对象

    UITouch *touch = [touches anyObject];

//    通过触摸对象获取手指的位置

    CGPoint point = [touch locationInView:touch.view];  

//    创建路径对象

    FFFBezierPath *path = [[FFFBezierPath alloc] init];

    [path moveToPoint:point];   

//    设置线的颜色

    path.lineColor = self.lineColor;

 

    if(self.lineWithBlock){

       path.lineWidth = self.lineWithBlock();

    }

//    下面的方法是无法获取到值的

//   path.lineWidth = self.lineWithed;

//   NSLog(@"%f",self.lineWithed);

    

//    path添加到数组中

    [self.paths addObject:path];

}

 

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{

 

//    获取触摸的对象

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:touch.view];

    

//    使用数组当中最后一个路径 来连线

    [[self.paths lastObject] addLineToPoint:point];

    

//    重绘

    [self setNeedsDisplay];

}

 

-(void)drawRect:(CGRect)rect{

//    遍历所有的路径渲染

    for (FFFBezierPath*path in self.paths) {

       [path.lineColor set];

       

//        设置样式

        [path setLineCapStyle:kCGLineCapRound];

        [path setLineJoinStyle:kCGLineJoinRound];

    //   渲染

       [path stroke];

//       NSLog(@"%@",path);

    }

}

 

@end

 

************************************************************************************

 

#import "ViewController.h"

#import "FFFPaintView.h"

@interface ViewController ()

 

@property (weak, nonatomic) IBOutlet FFFPaintView *paintView;

 

@property (weak, nonatomic) IBOutlet UISlider *slider;

 

@property (weak, nonatomic) IBOutlet UIButton *firstButton;

 

@end

 

@implementation ViewController

//-(IBAction)slider:(UISlider *)sender {

//   

//   self.paintView.lineWithed = sender.value;

//   

//}

 

- (IBAction)savePaint {

    

//    开启图片类型的上下文

    UIGraphicsBeginImageContextWithOptions(self.paintView.bounds.size, NO, 0);

    

//    获取当前上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

//    把当前view的样式,绘制到上下文当中

    [self.paintView.layer renderInContext:ctx];

    

//    保存到相册,同时从上下文中获取图片

    UIImageWriteToSavedPhotosAlbum(UIGraphicsGetImageFromCurrentImageContext(), nil, nil, nil);

    

//    关闭上下文

    UIGraphicsEndImageContext();

}

 

//清屏

-(IBAction)clearScreen{

    [self.paintView clearScreen];

}

//回退

-(IBAction)recede{

    [self.paintView recede];

}

//橡皮擦

-(IBAction)eraser{

    [self.paintView eraser];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.paintView setLineWithBlock:^CGFloat{

       return self.slider.value;

    }];

    self.paintView.lineColor = self.firstButton.backgroundColor;

}

 

- (IBAction)setColorSelected:(UIButton *)sender {

    self.paintView.lineColor = sender.backgroundColor;

}

@end

 

 

***********************************************************************************

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值