iOS - Quartz 2D 手势截屏绘制
1、绘制手势截屏
具体实现代码见 GitHub 源码 QExtension
QTouchClipView.h
@interface QTouchClipView : UIView /**
* 创建手势截屏视图控件,获取截屏结果
*
* @param view 截取图片的视图控件
* @param result 手势截屏结果
*
* @return 手势截屏视图控件
*/
+ (instancetype)q_touchClipViewWithView:(UIView *)view
clipResult:(void (^)(UIImage * _Nullable image))result; @end
QTouchClipView.m
@interface QTouchClipView () /// 截取图片的视图控件
@property (nonatomic, strong) UIView *baseView; /// 滑动手势结果
@property (nonatomic, copy) void (^resultBlock)(UIImage * _Nullable); /// 触摸开始结束点
@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint endP; @end @implementation QTouchClipView /// 创建手势截屏视图控件,获取截屏结果
+ (instancetype)q_touchClipViewWithView:(UIView *)baseView
clipResult:(void (^)(UIImage * _Nullable image))result { QTouchClipView *clipView = [[self alloc] initWithFrame:baseView.frame]; clipView.baseView = baseView;
clipView.resultBlock = result; return clipView;
} /// 初始化
- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) {
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}
return self;
} /// 触摸开始
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 获取触摸起始点位置
CGPoint startPoint = [touches.anyObject locationInView:self];
self.startP = startPoint;
} /// 触摸移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 获取触摸点位置
CGPoint touchPoint = [touches.anyObject locationInView:self];
self.endP = touchPoint; // 刷新视图
[self setNeedsDisplay];
} /// 触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 截取屏幕图片
UIGraphicsBeginImageContextWithOptions(self.baseView.bounds.size, NO, 0); CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.baseView.layer renderInContext:ctx]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 切割图片
CGFloat x = self.startP.x;
CGFloat y = self.startP.y;
CGFloat w = self.endP.x - x;
CGFloat h = self.endP.y - y; CGRect cutRect = CGRectMake(x * 2, y * 2, w * 2, h * 2); CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, cutRect);
UIImage *newImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage); // 返回截取结果
if (self.resultBlock) {
self.resultBlock(newImage);
} // 移除截取视图控件
[self removeFromSuperview];
self.startP = CGPointZero;
self.endP = CGPointZero; // 刷新视图
[self setNeedsDisplay];
} /// 触摸取消
- (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event {
[self touchesEnded:touches withEvent:event];
} /// 绘制触摸区域
- (void)drawRect:(CGRect)rect { CGFloat x = self.startP.x;
CGFloat y = self.startP.y;
CGFloat w = self.endP.x - x;
CGFloat h = self.endP.y - y; CGRect clipRect = CGRectMake(x, y, w, h); UIBezierPath *path = [UIBezierPath bezierPathWithRect:clipRect];
[[[UIColor whiteColor] colorWithAlphaComponent:0.2] setFill];
[path fill];
} @end
ViewController.m
// 创建手势截屏视图
QTouchClipView *touchClipView = [QTouchClipView q_touchClipViewWithView:self.imageView
clipResult:^(UIImage * _Nullable image) { // 获取处理截屏结果
if (image) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}]; // 添加手势截屏视图
[self.view addSubview:touchClipView];
效果

iOS - Quartz 2D 手势截屏绘制的更多相关文章
- iOS - Quartz 2D 第三方框架 Charts 绘制图表
1.Charts 简介 使用第三方框架 Charts 绘制 iOS 图表.GitHub 源码 Charts Charts 是一款用于绘制图表的框架,可以绘制柱状图.折线图.K线图.饼状图等.Chart ...
- iOS - Quartz 2D 下载进度按钮绘制
1.绘制下载进度按钮 具体实现代码见 GitHub 源码 QExtension QProgressButton.h @interface QProgressButton : UIButton /// ...
- iOS - Quartz 2D 二维绘图
1.Quartz 2D 简介 Quartz 2D 属于 Core Graphics(所以大多数相关方法的都是以 CG 开头),是 iOS/Mac OSX 提供的在内核之上的强大的 2D 绘图引擎,并且 ...
- 在iOS上增加手势锁屏、解锁功能
在iOS上增加手势锁屏.解锁功能 在一些涉及个人隐私的场景下,尤其是当移动设备包含太多私密信息时,为用户的安全考虑是有必要的. 桌面版的QQ在很多年前就考虑到用户离开电脑后隐私泄露的危险,提供了“离开 ...
- iOS - Quartz 2D 贝塞尔曲线
1.贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线.一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支 ...
- iOS中正确的截屏姿势
昨天写了个用到截屏功能的插件,结果问题不断,今天终于解决好了,把debug过程中所有尝试过的截屏方法都贴出来吧- 第一种 这是iOS 3时代开始就被使用的方法,它被废止于iOS 7.iOS的私有方法, ...
- iOS 点击按钮截屏
@interface CaptureViewController () @property (nonatomic, strong) UIImageView *backgrounView; //控制器背 ...
- ios摇一摇截屏代码
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- iOS - Quartz 2D 画板绘制
1.绘制画板 1.1 绘制简单画板 PaintBoardView.h @interface PaintBoardView : UIView @end PaintBoardView.m @interfa ...
随机推荐
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- Websocket原理及使用场景[转载]
WebSocket的使用场景 社交聊天.弹幕.多玩家游戏.协同编辑.股票基金实时报价.体育实况更新.视频会议/聊天.基于位置的应用.在线教育.智能家居等需要高实时的场景 由轮询到WebSocket 1 ...
- bzoj2876 [NOI2012]骑行川藏(拉格朗日乘数法)
题目描述 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因此在每天的骑行 ...
- CentOS6.8配置GO语言开发环境
Go语言是谷歌2009发布的第二款开源编程语言,Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. 鉴于原来越多的开源项 ...
- 揽货最短路径解决方案算法 - C# 蚁群优化算法实现
需求为(自己编的,非实际项目): 某配送中心进行揽货,目标客户数为50个客户,配送中心目前的运力资源如下: 现有车辆5台 单台运力最大行驶距离200千米 单台运力最大载重公斤1吨 问:运力怎样走法才能 ...
- python函数的面向对象——面向对象设计
通过几个函数式编号演进,理解面向对象设计 def01.py dog1 = { 'name':'元昊', 'gender':'母', 'type':'藏獒' } dog2 = { 'name':'李李' ...
- Freemarker的基本语法及入门基础
freemarker的基本语法及入门基础一.freemarker模板文件(*.ftl)的基本组成部分 1. 文本:直接输出的内容部分 2. 注释:不会输出的内容,格式为&l ...
- Web API 之承载宿主IIS,SelfHost,OwinSelfHost
Asp.Net WebAPI这个大家应该都不陌生,在我的理解范围中就是数据提供和交换的一个方式,相比与WCF,WS而言,更加的简单轻量,但是在部署web Api的时候,一般往往需要与a ...
- Mysql利用存储过程插入400W条数据
CREATE TABLE dept( /*部门表*/ deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0, /*编号*/ dname VARCHAR(20) NO ...
- C++学习笔记第三天:类、虚函数、双冒号
类 class Box { public: double length; // 盒子的长度 double breadth; // 盒子的宽度 double height; // 盒子的高度 }; 类成 ...