IOS 绘制画画板(封装上下文)
封装上下文
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 绘制画画板(封装上下文)的更多相关文章
- iOS 使用UIBezierPath类实现随手画画板
在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画, ...
- iOS - Quartz 2D 画板绘制
1.绘制画板 1.1 绘制简单画板 PaintBoardView.h @interface PaintBoardView : UIView @end PaintBoardView.m @interfa ...
- IOS 绘制基本图形(画文字、图片水印)
- (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...
- IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)
// 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...
- iOS_24_画画板(含取色板)
终于效果例如以下: 一.简单说明 1.使用一个数组 strokesArr(笔画数组)记录全部笔画.数组中保存的是一个个的笔画字典,一个字典就是一个笔画.笔画字典中有三项:笔画的大小.颜色.points ...
- android96 内存创建图片副本,画画板
package com.itheima.copy; import android.os.Bundle; import android.app.Activity; import android.grap ...
- Android简单开发的画画板
Android开发画画板要考虑得几个问题如下: 1 屏幕画板.画笔如何绘制问题 2 用户手指触摸屏幕画板监听事件,以及对应的几种状态处理问题 3 保存图片到SD卡,以及在系统相册打开时自动加载刚才的 ...
- Android简易实战教程--第二十四话《画画板》
今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- canvas画画板,canvas画五角星,canvas制作钟表、Konva写钟表
制作一个画画板,有清屏有橡皮擦有画笔可以换颜色 style样式 <head> <meta charset="UTF-8"> <title>画画板 ...
随机推荐
- Unix Tutorial Eight
1.UNIX 变量 变量是在运行时将信息从shell传递到程序的一种方式.程序在特定的变量中查找“在环境中”,如果发现它们将使用存储的值.有些是由系统设置的,另一些是由你设置的,还有一些是由shell ...
- 使用pods添加第三方的时候,出现ld: library not found for -lpop
ld: library not found for -lpop 错误,是在使用pods添加第三方的时候,出现的编译错误,同时伴随着的是error: linker command failed with ...
- 阿里maven镜像配置
setting.xml<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven< ...
- libaudit_plugin.so安装
#上传audit到mysql的plugin目录vim /etc/my.cnfplugin-load=AUDIT=libaudit_plugin.soaudit_json_file=1audit_jso ...
- 我的Android Studio配置
Android Studio固然好用,但是刚从Eclipse转移到Android Studio上难免有很多不便,现在集中整理一下AS与Eclipse的区别,使得AS更好用. 快捷键Keymap到Ecl ...
- Sharepoint 图片库字段名称(Title)和对应的内部名称(InternalName)
做项目遇到需要查看列表库的内部名称,因为用的是图片库,所以就列出所有对应关系,以备查看方便:
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- (转)深入浅出linux系统umask值及其对应的文件权限讲解
浅出linux系统umask值及其对应的文件权限讲解 原文:http://blog.51cto.com/oldboy/1060032 缘起:1.此文的撰写特别为感谢51cto的博客工作人员和领导,老男 ...
- vs2017 运行所选代码生成器时出错
[转]https://blog.csdn.net/sunshineyang1205/article/details/80211313 ASPNET MVC Core中像Controller中添加控制器 ...
- android 开发AlertDialog.builder对话框的实现
AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...