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 ...
随机推荐
- alertifyjs
<%@ page contentType="text/html; charset=UTF-8"%> <!DOCTYPE html PUBLIC "-// ...
- chrome下input文本框自动填充背景问题解决
chrome下input文本框会自动填充背景,只需要给文本框加一个样式即可解决问题 input:-webkit-autofill {-webkit-box-shadow: 0 0 0px 1000px ...
- [记录]Zabbix3.4配置监控Oracle12c的存活状态和表空间使用率
Zabbix3.4配置监控Oracle的存活状态和表空间使用率 1.安装zabbix3.4 agent: # rpm -ivh http://repo.zabbix.com/zabbix/3.4/rh ...
- C/C++语言简介之运算符
比较特别的是,比特右移(>>)运算符可以是算术(左端补最高有效位)或是逻辑(左端补 0)位移.例如,将 11100011 右移 3 比特,算术右移后成为 11111100,逻辑右移则为 0 ...
- 登录功能(MD5加密)
登录这个功能,是不管哪个项目都会用到的,登录做的好坏,安全性的保障将直接影响到整个系统的成败,尤其是一些安全性要求比较严格的项目 1.首先需要对密码进行加密,这里用到的是md5加密,需要在login. ...
- iOS中的定时器
据我所知,iOS中的定时器有两种.一个叫NSTimer,一个叫CADisplayLink.还有一种是使用GCD,不常用,这里就不介绍了. 下边说下两个定时器分别得用法: =============== ...
- Https访问
Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期.适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示.去年VPS侦探曾经说过Let's ...
- Centos下_MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: [Warning] Using a password on the command line interface can be insecure.
在阿里云服务器增加一个shell脚本定时备份数据库脚本执行任务时,测试性的执行了备份命令,如下 [root@iZ2ze503xw2q1fftv5rhboZ mysql_bak]# /usr/local ...
- Docker安装weblogic
Docker容器安装weblogic详细教程 前提:已经安装后Docker,并且能正常使用 (1)获取镜像: docker pull ismaleiva90/weblogic12 docker pu ...
- hihoCoder 1036 Trie图 AC自动机
题意:给定n个模式串和一个文本串,判断文本中是否存在模式串. 思路:套模板即可. AC代码 #include <cstdio> #include <cmath> #includ ...