IOS简单画板实现
效果图

设计要求
1、画笔能设置大小、颜色
2、有清屏、撤销、橡皮擦、导入照片功能
3、能将绘好的画面保存到相册
实现思路
1、画笔的实现,我们可以通过监听用户的 平移手势 中创建 UIBezierPath 来实现线条的绘制
2、撤销功能,我们先来看下撤销功能,我们会想到用一个数组队列将用户的每一次的笔画都加入到数组中,然后撤销的时候只需要将最后添加进去的笔画pop掉,重新绘制就可以了
3、清屏功能就简单了,只需要将上面说到的那个数组清空重新绘制下就可以了
4、导入照片功能,可以用系统的 UIImagePickerController 选取照片得到UIImage,然后再将 UIImage 绘制到屏幕中就可以了
5、保存到相册功能,可以使用 UIGraphicsGetImageFromCurrentImageContext 获取当前的图片上下文,得到屏幕画面的 UIImage ,然后通过 UIImageWriteToSavedPhotosAlbum 写入到相册
具体代码实现
1、先画个界面

2、因为我们绘制线条会用到 UIBezierPath ,并且要能可设置颜色,但是UIBezierPath是没有设置颜色的属性,所以我们这里需要简单扩展一下,创建一个继承于 UIBezierPath 的子类 DrawPath
//
// DrawPath.h
// 画板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
// #import <UIKit/UIKit.h> @interface DrawPath : UIBezierPath
// 画笔颜色
@property(nonatomic,retain)UIColor* pathColor; @end
这个子类只需要扩展一个属性,就是 pathColor 用来保存画笔的颜色
//
// DrawPath.m
// 画板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
// #import "DrawPath.h" @implementation DrawPath @end
DrawPath.m 里面不需要做其它功能
3、接到来我们对画板功能的实现封装一下,创建一个继承于UIView的 DrawView子类
//
// DrawView.h
// 画板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
// #import <UIKit/UIKit.h> @interface DrawView : UIView // 画线的宽度
@property(nonatomic,assign)CGFloat lineWidth; // 线条颜色
@property(nonatomic,retain)UIColor* pathColor; // 加载背景图片
@property(nonatomic,strong)UIImage* image; // 清屏
- (void)clear; // 撤销
- (void)undo; // 橡皮擦
- (void)eraser; // 保存
- (void)save; @end
//
// DrawView.m
// 画板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
// #import "DrawView.h"
#import "DrawPath.h" @interface DrawView() @property(nonatomic,strong) DrawPath* path; // 线的数组
@property(nonatomic,strong) NSMutableArray* paths; @end @implementation DrawView - (void)awakeFromNib{ [self setUp];
} - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
} // 重绘UI
- (void)drawRect:(CGRect)rect { for (DrawPath* path in self.paths) { if ([path isKindOfClass:[UIImage class]]) {
// 画图片
UIImage* image = (UIImage*)path;
[image drawInRect:rect];
}else{
// 画线 // 设置画笔颜色
[path.pathColor set]; // 绘制
[path stroke];
}
}
} // 懒加载属性
- (NSMutableArray*)paths{ if (_paths == nil) {
_paths = [NSMutableArray array];
}
return _paths;
} // 重写image属性
- (void)setImage:(UIImage *)image{ _image = image; // 将图片加入到线条数组中
[self.paths addObject:image]; [self setNeedsDisplay];
} #pragma mark - Init // 初始化
- (void)setUp{ // 添加平移手势
UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
[self addGestureRecognizer:panGes]; // 默认值
self.lineWidth = ;
self.pathColor = [UIColor blackColor];
} #pragma mark - Event // 平移事件
- (void)panGes:(UIPanGestureRecognizer*)ges{ // 获取当前点
CGPoint curPoint = [ges locationInView:self]; if (ges.state == UIGestureRecognizerStateBegan) { // 开始移动 // 创建贝塞尔曲线
_path = [[DrawPath alloc]init]; // 设置线条宽度
_path.lineWidth = _lineWidth; // 线条默认颜色
_path.pathColor = _pathColor; // 设置起始点
[_path moveToPoint:curPoint]; [self.paths addObject:_path];
} // 连线
[_path addLineToPoint:curPoint]; // 重绘
[self setNeedsDisplay];
} #pragma mark - Method // 清屏
- (void)clear{ [self.paths removeAllObjects]; [self setNeedsDisplay];
} // 撤销
- (void)undo{ [self.paths removeLastObject]; [self setNeedsDisplay];
} // 橡皮擦
- (void)eraser{ self.pathColor = [UIColor whiteColor]; [self setNeedsDisplay];
} // 保存
- (void)save{ // ---- 截图操作
// 开启上下文
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, ); // 获取当前上下文
CGContextRef context = UIGraphicsGetCurrentContext(); // 渲染图层到上下文
[self.layer renderInContext:context]; // 从上下文中获取图片
UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文
UIGraphicsEndImageContext(); // ---- 保存图片
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } // 图片保存方法,必需写这个方法体,不能会保存不了图片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ // 提示
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"保存成功" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
} @end
4、接下来就是如果使用这个画板类了,直接上代码吧
//
// ViewController.m
// 画板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
// #import "ViewController.h"
#import "DrawView.h" @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> // 画板
@property (weak, nonatomic) IBOutlet DrawView *drawView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; } #pragma mark - Event // 线条宽度变化
- (IBAction)lineWidthChange:(UISlider*)sender { _drawView.lineWidth = sender.value;
} // 线条颜色变化
- (IBAction)pathColorChange:(UIButton*)sender { _drawView.pathColor = sender.backgroundColor;
} // 清屏
- (IBAction)clearAction:(id)sender { [_drawView clear];
} // 撤销
- (IBAction)undoAction:(id)sender { [_drawView undo];
} // 橡皮擦
- (IBAction)eraserAction:(id)sender { [_drawView eraser];
} // 照片
- (IBAction)pickerPhotoAction:(id)sender { // 照片选择控制器
UIImagePickerController* picVC = [[UIImagePickerController alloc]init];
// 照片源
picVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 委托
picVC.delegate = self; [self presentViewController:picVC animated:YES completion:nil];
} // 保存
- (IBAction)saveAction:(id)sender { [_drawView save];
} #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{ // 设置图片
_drawView.image = image; // 关闭窗口
[self dismissViewControllerAnimated:YES completion:nil];
} @end
到这里就差不多了,这个小功能实现的基本思路与具体代码我都已经放上来了,大家如果还有哪里不清楚的可以留言喔~~
IOS简单画板实现的更多相关文章
- iOS 简单工厂模式
iOS 简单工厂模式 什么是简单工厂模式? 简单工厂模式中定义一个抽象类,抽象类中声明公共的特征及属性,抽象子类继承自抽象类,去实现具体的操作.工厂类根据外界需求,在工厂类中创建对应的抽象子类实例并传 ...
- iOS 简单引导界面
代码地址如下:http://www.demodashi.com/demo/11607.html 前言 现在很多APP在用户第一次用的时候,由于用户可能并不知道其中一些功能点的时候,这个时候就需要我们来 ...
- iOS: 为画板App增加 Undo/Redo(撤销/重做)操作
这个随笔的内容以上一个随笔为基础,(在iOS中实现一个简单的画板),上一个随笔实现了一个简单的画板: 今天我们要为这个画板增加Undo/Redo操作,当画错了一笔,可以撤销它,或者撤销之后后悔了, ...
- ios 简单的plist文件读写操作(Document和NSUserDefaults)
最近遇到ios上文件读写操作的有关知识,记录下来,以便以后查阅,同时分享与大家. 一,简单介绍一下常用的plist文件. 全名是:Property List,属性列表文件,它是一种用来存储串行化后的对 ...
- iOS简单快速集成Cordova
如果你对于什么是Cordova还不了解,可以先移步到我另一个文章:Cordoval在iOS中的运用整理 里面有详细的介绍跟如何搭建Cordova:而本文则是要介绍JiaCordova插件,如果你有一点 ...
- iOS 简单获取当前地理坐标
iOS 获取当前地理坐标 iOS获取当前地理坐标,很简单几句代码,但是如果刚开始不懂,做起来也会也会出现一些问题. 1.导入定位需要用到的库:CoreLocation.framwork ...
- iOS开发——高级篇——iOS涂鸦画板效果实现
一个简单的绘图应用,模仿苹果自带软件备忘录里的涂鸦功能 核心代码 #import "DrawView.h" #import "DrawPath.h" @inte ...
- iOS简单音乐实现、React-Native完整项目、仿闲鱼京东列表分页、语音识别、网络加载过度动画等源码
iOS精选源码 iOS快速入手语音识别.听写.评测.播报 网络加载数据的过渡动画(仿简书网页) iOS 封装跑马灯和轮播效果 crash防护组件,适用常见常用的数组,字典等crash保护 iOS:高仿 ...
- iOS简单动画
知识架构 CALayer 图层类 CABasicAnimation 基础动画 CAKeyFrameAnimation 帧动画 CATransition 转场动画 CAAnimationGroup 动画 ...
随机推荐
- 使用 flow.ci 快速发布你的项目文档
软件研发的协作过程中,文档是必不可少的一环,有需求文档.接口文档.使用文档等等.当开始写文档时,首先会遇到两个问题: team members 之间如何协作? 文档 OK 后如何分发,去哪里看?如何更 ...
- mdadm命令详解及实验过程
一.概念 mdadm是multiple devices admin的简称,它是Linux下的一款标准的软件 RAID 管理工具,作者是Neil Brown 二.特点 mdadm能够诊断.监控和收集详细 ...
- [Spark] - HashPartitioner & RangePartitioner 区别
Spark RDD的宽依赖中存在Shuffle过程,Spark的Shuffle过程同MapReduce,也依赖于Partitioner数据分区器,Partitioner类的代码依赖结构主要如下所示: ...
- Qt 地址薄 (一) 界面设计
实现一个简单的地址薄,功能包括:地址的添加.浏览.编辑.查找.输出文件等. 1 界面和元素 整个地址薄界面,可视为一个 AddressBook 类.其中的 Name.Address 以及两个编辑栏, ...
- java(jdk1.7) IO系列01之InputStream和OutputStream解析
1.InputStream和OutputStream简介 在java中InputStream和OutputStream分别代表字节输入流和字节输出流,表示以字节的方式来实现进程或者程序的通信,Inpu ...
- MySQL 修改最大连接数
方法一:进入MySQL安装目录 打开MySQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MySQ ...
- ios的300ms点击延时问题
一.什么是ios的300ms点击延时问题 ios的移动端页面对点击事件有300ms延时. 二.为什么存在这个问题 这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题 —— ...
- 【struts2】ActionContext与ServletActionContext
1 再探ActionContext 我们知道,ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,也称之为广义值栈.Struts2在每次执行Actio ...
- .NET基础笔记(C#)
闲着没事就把以前学习时的笔记拿出来整理了一下,个人感觉有点用,就想拿出来跟园友共享一下.有些基础性的内容比如基本概念.语法什么的就不发了. 内容:1.构造方法(函数) 2.继承 3.访问修饰符 ...
- 深入JSP学习
常规JSP JSP页面最终会由容器来生成Servlet类的,比如Tomcat容器生成JSP的Servlet类放在work目录里.因此在JSP里可以用很多简化的语法供容器使用,这篇就来整理一下. JSP ...