iOS8以上的新方法PhotoKit

监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html

PhotoKit 获取本机相册列表 PHAssetCollection有一个名为 Screenshots的智能相册,获取这里面最新的一张图片,即可获取用户刚刚截屏的图片

自己截取屏幕,兼容iOS7-iOS10:带状态栏

  • OTScreenshotHelper oc 开源库

    https://github.com/OpenFibers/OTScreenshotHelper

  • 直接使用 OTScreenshotHelper 截图效果如下:

    不知道为什么头部和底部的view是透明的,上下两个view都设置了毛玻璃效果,但是截图出来view背景透明

  • 目前解决方案是不带状态栏的,所以试过各种方案之后还是两个方案融合在一起:使用OTScreenshotHelper截图状态栏, 使用不带状态栏方案截取主体部分

1. 刚刚拷贝源码到swift项目

  • UIView+OTStatusBarReference.m
#import "UIView+ComOpenThreadOTScreenshotHelperStatusBarReference.h"
#import "ComOpenThreadOTScreenshotHelperSwizzleHelper.h"
// 这一行需要移动到 import 语句下面,不然会报库找不到
static UIView *statusBarInstance = nil;
  • OTScreenShotHelper.h
把#import <Foundation/Foundation.h>
增加:
#import <UIKit/UIKit.h>
解决UIView 提示找不到的问题

2. OTScreenShotHelper.h 中暴露mergeStatusBarToContext方法

这个方法在OTScreenShotHelper 其实已经实现了,就是没有暴露给外面调用

+ (void)mergeStatusBarToContext:(CGContextRef)context rect:(CGRect)rect screenshotOrientation:(UIInterfaceOrientation)o;

3. 在现有截图代码位置调用mergeStatusBarToContext

for subWindow in (UIApplication.shared.windows) {
context.saveGState()
context.translateBy(x: subWindow.center.x, y: subWindow.center.y)
context.concatenate(subWindow.transform)
context.translateBy(x: -subWindow.bounds.size.width * subWindow.layer.anchorPoint.x,
y: -subWindow.bounds.size.height * subWindow.layer.anchorPoint.y)
if (orientation == UIInterfaceOrientation.landscapeLeft) {
context.rotate(by: CGFloat(M_PI_2))
context.translateBy(x: 0, y: -imageSize.width)
}
else if (orientation == UIInterfaceOrientation.landscapeRight) {
context.rotate(by: -CGFloat(M_PI_2))
context.translateBy(x: -imageSize.width, y: 0)
}
else if (orientation == UIInterfaceOrientation.portraitUpsideDown) {
context.rotate(by: CGFloat(M_PI))
context.translateBy(x: -imageSize.width, y: -imageSize.height)
}
if ( subWindow.responds(to: #selector(UIWindow.drawHierarchy(in:afterScreenUpdates:))) ){
subWindow.drawHierarchy(in: subWindow.bounds, afterScreenUpdates: true)
} else {
subWindow.layer.render(in: context)
}
context.restoreGState() // 这部分是截图带有statusbar的关键,在这里把状态栏截图动态添加到了原有图片中,在未带状态栏截图代码如下位置添加如下代码即可
let statusBarRect = CGRect(x: 0, y: 0, width: AppWidth, height: 20)
OTScreenshotHelper.mergeStatusBar(to: context, rect: statusBarRect, screenshotOrientation: orientation)
// End } let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return image

未带状态栏截图代码参见:

https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167


自己截取屏幕,兼容iOS7-iOS10:不带状态栏

创建两个UIImage
UIImage *image1 = [UIImage imageNamed:@"iOSDevTip"];
UIImage *image2 = [UIImage imageNamed:@"CodePush"];
创建UIImage的方法有很多种,我们就简单的通过imageNamed:方法来创建。 合并之后的size
CGSize size = CGSizeMake(image1.size.width + image2.size.width, image1.size.height); 合并两个UIImage,需要计算合并之后的size。假设这两个UIImage的高度是是相同的,把他们的宽度相加,得到合并之后的UIImage的size。
合并方法有了UIImage和size接下来就是把两个UIImage合并,方法如下: UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, size.height)];
[image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, size.height)];
UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

iOS 截屏分享(包含状态栏与不包含状态栏)的更多相关文章

  1. iOS截屏并修改截图然后分享的功能实现

    一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...

  2. ios截屏代码[转]

    http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...

  3. iOS截屏代码

    转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...

  4. iOS截屏保存至相册

    #pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...

  5. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  6. iOS截屏功能

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  7. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  8. ios截屏事件监听

    目的:实现截屏反馈,类似支付宝的截屏上传反馈功能. 1.注册全局通知,在Appdelegate中注册截屏监听通知 - (void)registNotification{ [[NSNotificatio ...

  9. iOS 截屏,openGL ES 截图,以及像素颜色判断

    代码整理了2种截图,类似.(没苹果自带那种截图彻底) 方法一: +(UIImage *)fullScreenshots{ UIWindow *screenWindow = [[UIApplicatio ...

随机推荐

  1. linux环境中,ssh登录报错,Permission denied, please try again.

    问题描述: 今天早上一个同事反应一个问题,通过ssh登录一台测试机的时候,发现两个账号,都是普通账号,一个账号能够登录, 另外一个账号无法登录.问他之前有做过什么变更吗,提到的就是之前有升级过open ...

  2. 【linux】 linux 禁止ping

    linux 禁止 ping 一.修改内核参数 1.临时允许PING操作的命令为:echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all 2.永久允许PIN ...

  3. 马尔科夫链蒙特卡洛(Markov chain Monte Carlo)

    (学习这部分内容大约需要1.3小时) 摘要 马尔科夫链蒙特卡洛(Markov chain Monte Carlo, MCMC) 是一类近似采样算法. 它通过一条拥有稳态分布 \(p\) 的马尔科夫链对 ...

  4. FileSaver.js 浏览器导出Excel文件

    限制一:不同浏览器对 blob 对象有不同的限制 具体看看下面这个表格(出自 FileSaver.js): Browser Constructs as Filenames Max Blob Size ...

  5. eclipse debug Liunx服务器上的svn项目

    1.本地项目提交到svn上,以保证本地代码与服务器代码相同 2.开启服务器debug端口 3.使用root账号重新部署服务器项目并监听catalina.out sh /home/p/deploy/gt ...

  6. html+jquery制作网页地图

    http://jvectormap.com/ <!--StartFragment --> JVectorMap 是一个显示矢量地图的jQuery插件.它使用 SVG 在Firefox 3 ...

  7. LHC大神问的矩阵转置问题

    数学中线性代数中提到的矩阵转置,其实在我们的业务场景中也有需要的地方,比如LHC大神问到的这个问题 那么如何进行行列转换呢? 代码如下: <?php $array=array( '部门1'=&g ...

  8. git push 问题汇总

    Q:git push时卡死 这个问题找的快要放弃的时候... A: git config --global http.postBuffer [via] Q:git push 报错 Counting o ...

  9. 深度缓存ZBuffer线性化

    double linearizeDepth(double nearz,double farz,double depth) { depth = 2.0 * depth - 1.0; return (2. ...

  10. mysql5.6.34-debug Source distribution在树莓派下编译的几个错误

    raspberrypi下编译mysql5.6 debug版源码. 1. 启动错误 和mysqld相关的文件及文件夹权限必须设置为mysql用户可读可写可执行,特别是/var/run/mysqld/目录 ...