iOS当前屏幕截屏
需求描述:
有两个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当前屏幕截屏的更多相关文章
- iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)
iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 ...
- iOS检测用户截屏并获取所截图片
iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...
- DDGScreenShot — 复杂屏幕截屏(如view ScrollView webView wkwebView)
写在前面 最近有这么一个需求,分享页面,分享的是web订单截图,既然是web 就会有超出屏幕的部分, 生成的图片还要加上我们的二维码,这就涉及到图片的合成了. 有了这样的需求,就是各种google.也 ...
- iOS模块器截屏闪退
最近不知道什么原因,iOS模块器截屏命令点击模拟器就闪退,在此记录下在命令行截屏操作: 第一步:打开对应的模拟器 第二步:模拟器缩放比为100% 第三步:输入以下命令,001.jpg为要保存的文件名 ...
- java实现屏幕截屏功能
最近在项目中遇到这样一个需求,用户生成推广海报想要发送给朋友,但是推广海报是用html网页写的,这时候想要分享给朋友的话只能用户自己手机截图,显然这样的用户体验是不友好的,如果可以给用户一个按钮实现一 ...
- IOS 上架要求视频及屏幕截屏
客户提供上架的资料 1.IOS 上架要求视频演示,录制一段视频,上传到优酷,需要url连接. 2.手机截屏,每个尺寸5张.5s/6/6p *5=15张.截屏图片分辨率. iPhone4s手机 3.5I ...
- 【iOS】Quartz2D截屏
一.简单说明 在程序开发中,有时候需要截取屏幕上的某一块内容,比如捕鱼达人游戏.如图: 完成截屏功能的核心代码:- (void)renderInContext:(CGContextRef)ctx;调用 ...
- iOS 中捕获截屏操作
转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...
- iOS 百度地图截屏
关于百度地图截屏的问题,发现不能用常用的方法进行载屏,常用的截屏方法所得到的图片地图瓦片底图会显示空白,网上给出的答案是这样的 :因为百度地图不是用UIKit实现的,所以得不到截图! 不过通过Open ...
随机推荐
- Linux下Mysql的安装步骤
(1).下载安装包 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar [roo ...
- zabbix通过snmp监控网络设备
首先需要在zabbix的server端或proxy端安装snmpd服务 安装: yum -y install net-snmp* 查看版本: [root@Check3 ~]# snmpd -v NET ...
- SpringBoot 悲观锁 与 乐观锁
乐观所和悲观锁策略 悲观锁:在读取数据时锁住那几行,其他对这几行的更新需要等到悲观锁结束时才能继续 . 乐观所:读取数据时不锁,更新时检查是否数据已经被更新过,如果是则取消当前更新,一般在悲观锁的等待 ...
- python数据可视化(持续更新)
1.折线图 import numpy as np import matplotlib.pyplot as plt input_values = [1, 2, 3, 4, 5] s = [1, 4, 9 ...
- ngnix进阶
ngnix进阶 nginx: [warn] duplicate MIME type "text/html" in /usr/local/nginx/conf/nginx.conf: ...
- ubuntu 14.04 建立wifi热点
昨天突然想起来我可以用笔记本搞一个热点这样我的手机就不用上流量了,但是手机死活搜不到建好的信号,目前的解决方案如下: 直接用ubuntu自带的创建wifi网络功能是不好使的,因为android系统不支 ...
- gitlab库迁移
gitlab 迁移 gitlab上一共有两个分之,一级提交记录. git clone --bare http://111.222.333.xxx/jiqing/test.git 执行成功后,会多一个t ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is
在复制xml文件进行修改的时候,我经常将不小心对原文件进行修改,而导致创建bean出错.报错如下所示: Exception sending context initialized event to l ...
- 【spark】文件读写和JSON数据解析
1.读文件 通过 sc.textFile(“file://") 方法来读取文件到rdd中. val lines = sc.textFile("file://")//文件地 ...
- hdu1845
题解: 只要输出n/2即可 代码: #include<cstdio> #include<cmath> #include<cstring> #include<a ...