iOS学习笔记-063.画板

来源:互联网 发布:java编程题及答案 编辑:程序博客网 时间:2024/05/21 11:17

  • 画板
    • 一简要说明
    • 二代码
      • ViewControllerm
      • WMViewm
    • 三图示

画板

一、简要说明

保存的是路径 UIBezierPath

二、代码

1.ViewController.m

////  ViewController.m//  03_UIView54_画板////  Created by 杞文明 on 2016/04/18 07:59:41   星期一//  Copyright © 2016年 杞文明. All rights reserved.//#import "ViewController.h"#import "WMView.h"#import "MBProgressHUD+NJ.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet WMView *wmVIew;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];}//回退- (IBAction)backView:(id)sender {    [self.wmVIew backImage];}//清除- (IBAction)clearView:(id)sender {    [self.wmVIew clearImage];}//保存- (IBAction)savaView:(id)sender {    UIImage *image = [self.wmVIew getImage];    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{    if (error) {        [MBProgressHUD showError:@"保存失败"];    } else {        [MBProgressHUD showSuccess:@"保存成功"];    }}@end

2.WMView.m

////  WMView.m//  03_UIView54_画板////  Created by 杞文明 on 2016/04/18 07:46:51   星期一//  Copyright © 2016年 杞文明. All rights reserved.//#import "WMView.h"@interface WMView ()@property (nonatomic, strong) NSMutableArray *paths;@end@implementation WMView-(NSMutableArray*)paths{    if (_paths==nil) {        _paths = [[NSMutableArray alloc]init];    }    return _paths;}//按下- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //1.获取touch    UITouch * touch = [touches anyObject];    //2.获取点    CGPoint point = [touch locationInView:touch.view];    //3.创建路径    UIBezierPath * path = [UIBezierPath bezierPath];    [path moveToPoint:point];    //4.保存路径    [self.paths addObject:path];}//移动- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //1.获取touch    UITouch * touch = [touches anyObject];    //2.获取点    CGPoint point = [touch locationInView:touch.view];    //3.获取到最后一个路径    UIBezierPath * path = [self.paths lastObject];    //4.添加点    [path addLineToPoint:point];    //5.需要绘制    [self setNeedsDisplay];}//抬起- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self touchesMoved:touches withEvent:event];}//绘画- (void)drawRect:(CGRect)rect {    for ( UIBezierPath *path in self.paths) {        [path stroke];    }}//回退- (void)backImage{    [self.paths removeLastObject];    [self setNeedsDisplay];}//清除-(void)clearImage{    [self.paths removeAllObjects];    [self setNeedsDisplay];}//获取图片-(UIImage*)getImage{    //1.获取图片上下文    UIGraphicsBeginImageContext(self.bounds.size);    //2.绘制到图片上下文中    [self.layer drawInContext:UIGraphicsGetCurrentContext()];    //3.返回UIImage    return UIGraphicsGetImageFromCurrentImageContext();}@end

三、图示

这里写图片描述

0 0
原创粉丝点击