需求描述:

有两个ViewController 我们记做 A、B ,其中B controller只是显示下半部分;

如下图效果:

实现这种的方案很多,可以用添加View方法,  也可以用UIWindows 来实现。

但是我这边是想用presentViewController 实现,但是A present  B之后,之前的A就会消失,不会和B 覆盖显示,因此就相当了截取A试图之后在present B。

具体看看实现方案吧:

方法一:直接截屏当前视图

这个方法获取的到图片不会失真

 -(UIImage *)captureImageFromViewLow:(UIView *)orgView {
//获取指定View的图片
UIGraphicsBeginImageContextWithOptions(orgView.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[orgView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

方法二:获取当前window 层截图(当然在webView情况下是没法截图的)

/**
全屏截图方法
@return 获取指定View 的截屏结果
*/
-(UIImage *)fullScreenImage{
UIWindow *windows = [UIApplication sharedApplication].keyWindow;
CGFloat scale = ([UIScreen mainScreen].scale);
/*下面方法,
*第一个参数表示区域大小。
*第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。
*第三个参数就是屏幕密度了
*/
UIGraphicsBeginImageContextWithOptions(windows.size, YES, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[windows.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

方法三:webView,scroller截图

/**
* 根据视图尺寸获取视图截屏(一屏无法显示完整),适用于UIScrollView UITableviewView UICollectionView UIWebView
*
* @return UIImage 截取的图片
*/
- (UIImage *)scrollViewCutter:(UIScrollView *)scrollView
{
//保存
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
UIImage *image = [self viewCutter:scrollView];
//还原数据
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
return image;
} /**
获取指定的View的屏幕 @return 获取指定View 的截屏结果
*/
-(UIImage *)viewCutter:(UIView*)view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size,NO,[[UIScreen mainScreen] scale]);
// 方法一 有时导航条无法正常获取
// [self.layer renderInContext:UIGraphicsGetCurrentContext()];
// 方法二 iOS7.0 后推荐使用
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; UIImage*img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

方法四:递归每个windows层的截屏(全屏截图)

如果碰到webViewController 会发现前面两个方法都是无法实现的,那么请看下面方法:

/**
* 返回截取到的图片
*
* @return UIImage *
*/
- (UIImage *)imageWithScreenshot
{
NSData *imageData = [self dataWithScreenshotInPNGFormat];
return [UIImage imageWithData:imageData];
} /**
* 截取当前屏幕
*
* @return NSData *
*/
- (NSData *)dataWithScreenshotInPNGFormat
{
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation))
imageSize = [UIScreen mainScreen].bounds.size;
else
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, 0, -imageSize.width);
}
else if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
}
else
{
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
} UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return UIImagePNGRepresentation(image);
}

综合这四个方案,最终我选择了方案四

iOS当前屏幕截屏的更多相关文章

  1. iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)

    iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 ...

  2. iOS检测用户截屏并获取所截图片

    iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...

  3. DDGScreenShot — 复杂屏幕截屏(如view ScrollView webView wkwebView)

    写在前面 最近有这么一个需求,分享页面,分享的是web订单截图,既然是web 就会有超出屏幕的部分, 生成的图片还要加上我们的二维码,这就涉及到图片的合成了. 有了这样的需求,就是各种google.也 ...

  4. iOS模块器截屏闪退

    最近不知道什么原因,iOS模块器截屏命令点击模拟器就闪退,在此记录下在命令行截屏操作: 第一步:打开对应的模拟器 第二步:模拟器缩放比为100% 第三步:输入以下命令,001.jpg为要保存的文件名  ...

  5. java实现屏幕截屏功能

    最近在项目中遇到这样一个需求,用户生成推广海报想要发送给朋友,但是推广海报是用html网页写的,这时候想要分享给朋友的话只能用户自己手机截图,显然这样的用户体验是不友好的,如果可以给用户一个按钮实现一 ...

  6. IOS 上架要求视频及屏幕截屏

    客户提供上架的资料 1.IOS 上架要求视频演示,录制一段视频,上传到优酷,需要url连接. 2.手机截屏,每个尺寸5张.5s/6/6p *5=15张.截屏图片分辨率. iPhone4s手机 3.5I ...

  7. 【iOS】Quartz2D截屏

    一.简单说明 在程序开发中,有时候需要截取屏幕上的某一块内容,比如捕鱼达人游戏.如图: 完成截屏功能的核心代码:- (void)renderInContext:(CGContextRef)ctx;调用 ...

  8. iOS 中捕获截屏操作

    转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...

  9. iOS 百度地图截屏

    关于百度地图截屏的问题,发现不能用常用的方法进行载屏,常用的截屏方法所得到的图片地图瓦片底图会显示空白,网上给出的答案是这样的 :因为百度地图不是用UIKit实现的,所以得不到截图! 不过通过Open ...

随机推荐

  1. 剑指offer编程题66道题 36-66

    36.两个链表的第一个公共节点 题目描述 输入两个链表,找出它们的第一个公共结点. 1.具有重合节点的两个链表是一个Y字性,用两个堆栈放这两个链表,从尾部开始遍历,直到遍历到最后一个重合节点. 这种算 ...

  2. Python面试题之Python正则表达式指南

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  3. Book Review of “The practice of programming” (Ⅲ)

    The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...

  4. 一个线程知识点, 一个MongoDB的知识点

    //WINForm窗体中切换前后台线程执行任务: protected void RunOnUI(Action action) { Invoke(action); } protected void Ru ...

  5. 智能DNS

    DNS查找下一个服务的地址   一.智能DNS APP通过域名访问DNS服务器,DNS根据域名对应一组IP中随机选择一个,发给APP.从这个意义说智能DNS,智能DNS相当一个七层的负载均衡. 二.H ...

  6. ELK分布式日志收集搭建和使用

    大型系统分布式日志采集系统ELK全框架 SpringBootSecurity1.传统系统日志收集的问题2.Logstash操作工作原理3.分布式日志收集ELK原理4.Elasticsearch+Log ...

  7. mysql入门语句

    连接 1.mysql -h localhost -u root -p ******(回车) 2.mysql -h localhost -u root -p(回车) ****** 3.mysql -u ...

  8. Java中的逻辑运算符

    逻辑运算符主要用于进行逻辑运算.Java 中常用的逻辑运算符如下表所示: 我们可以从“投票选举”的角度理解逻辑运算符: 1. 与:要求所有人都投票同意,才能通过某议题 2. 或:只要求一个人投票同意就 ...

  9. django Models 常用的字段和参数

    1.字段 CharField IntegerField floatField DateTimeField DateField DecimalField 2.参数 null default choice ...

  10. AutoCompleteTextView的使用

    AutoCompleteTextView的使用 一.简介 1.AutoCompleteTextView的作用 2.AutoCompleteTextView的类结构图 也就是拥有EditText的各种功 ...