基于Quartz 2D实现截图功能
2015-10-22 20:04
337 查看
对于手机上的截屏功能,使用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
相关文章推荐
- 最长公共子序列问题
- Android 4.1 APP中的static变量即使在APP退出后仍然不会被擦除
- 月光博客:我的知识管理工具列表(强烈推荐收藏)
- 第四次作业——个人作业——软件案例分析
- python内置函数大全
- SparkPi源码解读
- 初次认识NoSQL
- Android自定义View
- 九度OJ 1085:求root(N, k) (迭代)
- python3中的range函数
- 九度OJ 1085:求root(N, k) (迭代)
- TortoiseGit disconnected: no supported authentication methods available(server sent:publickey)
- POJ DP训练计划
- 新思路团队-CSDN高校俱乐部
- UVA_11646_AthleticsTrack
- POJ2184Cow Exhibition (01背包变形)
- sicily 1139. 电路稳定性
- Welcome to JAVA!(第8章课后习题)
- Java学习笔记(二)——变量与常量
- 牙齿讲座记录