基于Quartz 2D实现截图功能
来源:互联网 发布:java单例模式添加数据 编辑:程序博客网 时间:2024/06/05 03:16
对于手机上的截屏功能,使用Quartz 2D绘制可以完成该功能,下面奉上小Demo仅供参考:
@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.png"]];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.记录当前手指的位置 UITouch *touch = [touches anyObject]; _beginPoint = [touch locationInView:self.view]; // 2.创建可一个可视的遮罩视图 if (_maskView == nil) { _maskView = [[UIView alloc] initWithFrame:CGRectZero]; _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3]; // 设置描边 _maskView.layer.borderWidth = 1; _maskView.layer.borderColor = [UIColor blackColor].CGColor; } // 3.设置位置 _maskView.frame = CGRectMake(_beginPoint.x, _beginPoint.y, 1, 1); // 添加到当前视图上 [self.view addSubview:_maskView];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.获取手指当前的位置计算出视图显示的大小 // 1.1 获取当前手指的位置 UITouch *_moveTouch = [touches anyObject]; CGPoint _movePoint = [_moveTouch locationInView:self.view]; // 1.2 当前遮罩视图的大小 float width = _movePoint.x - _beginPoint.x; float height = _movePoint.y - _beginPoint.y; // 1.3 设置给遮罩视图 _maskView.frame = CGRectMake(_beginPoint.x, _beginPoint.y, width, height); }- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.获取当前视图的frame // 2.移除当前遮罩视图 [_maskView removeFromSuperview]; // 3.获取指定区域内容内容 // 设置lay的锚点位置 self.view.frame = CGRectMake(-_maskView.frame.origin.x, -_maskView.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height); // 3.1 开始绘制图片画布 UIGraphicsBeginImageContextWithOptions(_maskView.frame.size, YES, 0); [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; // 3.2 获取整张图片大小 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 3.3 结束图片绘制 UIGraphicsEndImageContext(); self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); // 保存到本地相册 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}// 相册保存完成调用的方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error == nil) { NSLog(@"截图图片成功"); } else { NSLog(@"保存失败"); }}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.移除当前遮罩视图}/** * 截取部分图像 * **/- (UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool{ /*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/ float imgwidth = image.size.width; float imgheight = image.size.height; float viewwidth = mCGRect.size.width; float viewheight = mCGRect.size.height; CGRect rect; if(centerBool) rect = CGRectMake((imgwidth-viewwidth)/2, (imgheight-viewheight)/2, viewwidth, viewheight); else{ if (viewheight < viewwidth) { if (imgwidth <= imgheight) { rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth); }else { float width = viewwidth*imgheight/viewheight; float x = (imgwidth - width)/2 ; if (x > 0) { rect = CGRectMake(x, 0, width, imgheight); }else { rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth); } } }else { if (imgwidth <= imgheight) { float height = viewheight*imgwidth/viewwidth; if (height < imgheight) { rect = CGRectMake(0, 0, imgwidth, height); }else { rect = CGRectMake(0, 0, viewwidth*imgheight/viewheight, imgheight); } }else { float width = viewwidth*imgheight/viewheight; if (width < imgwidth) { float x = (imgwidth - width)/2 ; rect = CGRectMake(x, 0, width, imgheight); }else { rect = CGRectMake(0, 0, imgwidth, imgheight); } } } } CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect); CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef)); UIGraphicsBeginImageContext(smallBounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, smallBounds, subImageRef); UIImage* smallImage = [UIImage imageWithCGImage:subImageRef]; UIGraphicsEndImageContext(); return smallImage;}@end
0 0
- 基于Quartz 2D实现截图功能
- Quartz 2D 常用简单功能汇总
- Quartz 2D 常用简单功能汇总
- ios-day15-04(Quartz 2D之屏幕截图、并将截图保存到文件中)
- ios-day14-04(Quartz 2D之裁剪功能的实现)
- 基于java的后台截图功能的实现
- 截图功能的实现
- winform实现截图功能
- 简单截图功能实现
- Android截图功能实现
- winform实现截图功能
- android实现截图功能
- android实现截图功能
- 截图功能实现
- 屏幕截图功能实现
- Android实现截图功能
- ios 自由批注功能的实现 如何实现在view上自由绘图 bitmap Quartz 2D 缓存位图
- Quartz 2D 简单应用【打水印】&&【图片裁剪】&&【屏幕截图】
- 可重入与线程安全(Reentrancy and Thread-Safety)
- leetcode Sum Root to Leaf Numbers
- 牛客网 | 包含min函数的栈
- UVA_11817_TunnellingTheEarth
- (189)丢失的第一个正整数
- 基于Quartz 2D实现截图功能
- usaco4.1.1Beef McNuggets
- 数据库的几个概念:主键,外键,索引,唯一索引
- Maven安装配置及WEB工程构建
- Mysql中存在索引但是不能使用索引的典型情况
- keepalived安装部署及Flume+keepalived高可用
- Problem of Precision(矩阵快速幂)
- 指针学习笔记(1)——运算符*
- 测试功能