封装上下文

UIImage (CaptureView).h / .m

@interface UIImage (CaptureView)

+ (UIImage *)captureImageWithView:(UIView *)view;
@end
#import "UIImage+captureView.h"

@implementation UIImage (CaptureView)

+ (UIImage *)captureImageWithView:(UIView *)view
{
// 1.创建bitmap上下文
UIGraphicsBeginImageContext(view.frame.size);
// 2.将要保存的view的layer绘制到bitmap上下文中
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// 3.取出绘制号的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
return newImage;
} @end

画画板

NJView.h / .m

@interface NJView : UIView

- (void)clearView;
- (void)backView;
@end
#import "NJView.h"

@interface NJView ()

@property (nonatomic, strong) NSMutableArray *paths;

@end

@implementation NJView

- (NSMutableArray *)paths
{
if (_paths == nil) {
_paths = [NSMutableArray array];
}
return _paths;
} // 开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ // 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint startPoint = [touch locationInView:touch.view]; // 3.当用户手指按下的时候创建一条路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 3.1设置路径的相关属性
[path setLineJoinStyle:kCGLineJoinRound];
[path setLineCapStyle:kCGLineCapRound];
[path setLineWidth:]; // 4.设置当前路径的起点
[path moveToPoint:startPoint];
// 5.将路径添加到数组中
[self.paths addObject:path]; }
// 移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint movePoint = [touch locationInView:touch.view]; // 3.取出当前的path
UIBezierPath *currentPaht = [self.paths lastObject];
// 4.设置当前路径的终点
[currentPaht addLineToPoint:movePoint]; // 6.调用drawRect方法重回视图
[self setNeedsDisplay]; } // 离开view(停止触摸)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ [self touchesMoved:touches withEvent:event];
/*
// 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint endPoint = [touch locationInView:touch.view]; // 3.取出当前的path
UIBezierPath *currentPaht = [self.paths lastObject];
// 4.设置当前路径的终点
[currentPaht addLineToPoint:endPoint]; // 6.调用drawRect方法重回视图
[self setNeedsDisplay];
*/ } // 画线
- (void)drawRect:(CGRect)rect
{ [[UIColor redColor] set];
// 边路数组绘制所有的线段
for (UIBezierPath *path in self.paths) {
[path stroke];
} } - (void)test
{
/*
// 1.创建一条路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 20, 20);
CGPathAddLineToPoint(path, NULL, 100, 100); CGMutablePathRef path2 = CGPathCreateMutable();
CGPathMoveToPoint(path2, NULL, 120, 120);
CGPathAddLineToPoint(path2, NULL, 200, 200); CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.将路径添加到上下文中
CGContextAddPath(ctx, path);
CGContextAddPath(ctx, path2); // 3.渲染
CGContextStrokePath(ctx);
*/ /*
// UIBezierPath == CGMutablePathRef // 1.创建一条路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 2.给路径设置起点和重点
[path moveToPoint:CGPointMake(20, 20)];
[path addLineToPoint:CGPointMake(100, 100)]; [path setLineCapStyle:kCGLineCapRound];
[path setLineWidth:10];
[path setLineJoinStyle:kCGLineJoinRound]; // 3.渲染路径
[path stroke]; // 1.创建一条路径
UIBezierPath *path2 = [UIBezierPath bezierPath];
// 2.给路径设置起点和重点
[path2 moveToPoint:CGPointMake(120, 120)];
[path2 addLineToPoint:CGPointMake(200, 200)]; [path2 stroke];
*/
} - (void)clearView
{
[self.paths removeAllObjects];
[self setNeedsDisplay];
}
- (void)backView
{
[self.paths removeLastObject];
[self setNeedsDisplay];
}

调用画画板到控制器

@interface NJViewController ()
/**
* 清屏
*/
- (IBAction)clearBtnClick;
/**
* 回退
*/
- (IBAction)backBtnClick;
/**
* 保存
*/
- (IBAction)saveBtnClick; @property (weak, nonatomic) IBOutlet NJView *customView;
@end @implementation NJViewController
- (IBAction)clearBtnClick {
[self.customView clearView];
} - (IBAction)backBtnClick { [self.customView backView];
} - (IBAction)saveBtnClick { UIImage *newImage = [UIImage captureImageWithView:self.customView];
// 4.保存到相册
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[MBProgressHUD showError:@"保存失败"];
}else
{
[MBProgressHUD showSuccess:@"保存成功"];
}
}
@end

IOS 绘制画画板(封装上下文)的更多相关文章

  1. iOS 使用UIBezierPath类实现随手画画板

    在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画, ...

  2. iOS - Quartz 2D 画板绘制

    1.绘制画板 1.1 绘制简单画板 PaintBoardView.h @interface PaintBoardView : UIView @end PaintBoardView.m @interfa ...

  3. IOS 绘制基本图形(画文字、图片水印)

    - (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...

  4. IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)

    // 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...

  5. iOS_24_画画板(含取色板)

    终于效果例如以下: 一.简单说明 1.使用一个数组 strokesArr(笔画数组)记录全部笔画.数组中保存的是一个个的笔画字典,一个字典就是一个笔画.笔画字典中有三项:笔画的大小.颜色.points ...

  6. android96 内存创建图片副本,画画板

    package com.itheima.copy; import android.os.Bundle; import android.app.Activity; import android.grap ...

  7. Android简单开发的画画板

    Android开发画画板要考虑得几个问题如下: 1 屏幕画板.画笔如何绘制问题 2 用户手指触摸屏幕画板监听事件,以及对应的几种状态处理问题 3  保存图片到SD卡,以及在系统相册打开时自动加载刚才的 ...

  8. Android简易实战教程--第二十四话《画画板》

    今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  9. canvas画画板,canvas画五角星,canvas制作钟表、Konva写钟表

    制作一个画画板,有清屏有橡皮擦有画笔可以换颜色 style样式 <head> <meta charset="UTF-8"> <title>画画板 ...

随机推荐

  1. 管理时间TED语录

    When people find out I write about time management, They assume two things. One is that I'm always o ...

  2. 如何使用ros命令行显示图片

    rosrun image_view image_view image:=[TOPIC] 注意:每次只能显示一个UI.不能在一条命令中订阅多个节点.

  3. 数据绑定—Source(绑定到静态类的静态属性)

    <UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...

  4. Perfect service(树形dp)

    Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...

  5. [Xcode 实际操作]三、视图控制器-(10)在Storyboard中使用图像视图控件

    目录:[Swift]Xcode实际操作 本文将演示常用的图像视图控件在故事板中的使用. 打开故事板文件[Main.storyboard]点击选择视图控制器的根视图. 点击库图标,打开控件库面板. 在控 ...

  6. JMETER CSS JQUERY EXTRACTOR

    我想如果你在这里,你可能已经访问了我们关于变量提取的JMeter系列: XPath Extractor:使用XPath Expressions从XML响应中提取内容, Regexp Extractor ...

  7. JS实现拖拽功能

    本文代码地址(第一节):https://github.com/dirstart/js-exam/blob/master/%E6%8B%96%E6%8B%BDdiv1.html 第二节:https:// ...

  8. Django基础(3)----模型层-单表操作,多表创建

    昨日内容回顾: 1. {% include '' %} 2. extend base.html: <html> ..... ..... ..... {% block content%} { ...

  9. list倒序删除

    public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); ...

  10. HDU 4143 A Simple Problem 分解因式

    求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...