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. 好用的图片缩放JS

    <!DOCTYPE HTML> <meta charset="UTF-8"> <head> <script src="jquer ...

  2. oracle long类型转换成varchar2

    CREATE OR REPLACE FUNCTION LONG_TO_CHAR( in_rowid rowid,in_owner varchar,in_table_name varchar,in_co ...

  3. 5 -- Hibernate的基本用法 --4 6 Hibernate事务属性

    事务也是Hibernate持久层访问的重要方面,Hibernate不仅提供了局部事务支持,也允许使用容器管理的全局事务. Hibernate关于事务管理的属性: ⊙ hibernate.transac ...

  4. redis make错误处理

    cc: ../deps/hiredis/libhiredis.a: No such file or directory cc: ../deps/lua/src/liblua.a: No such fi ...

  5. openvpn记住用户名和密码,自动连接

    1, 打开openvpn安装目录 2, 在config目录中, 找到VPN服务器的配置文件, 我的是config.ovpn,将 auth-user-pass (若已经存在)改为 auth-user-p ...

  6. 【代码审计】iCMS_v7.0.7 apps.admincp.php页面存在SQL注入漏洞分析

      0x00 环境准备 iCMS官网:https://www.icmsdev.com 网站源码版本:iCMS-v7.0.7 程序源码下载:https://www.icmsdev.com/downloa ...

  7. FTP匿名访问修复方法

    window2003 window2008

  8. iOS开发--设置UIButton

    1.设置title [btn setTitle: @"search" forState: UIControlStateNormal]; 2.设置字体 //[btn setFont: ...

  9. php-fpm 配置进程池

    什么是 php-fpm :php 是作为一个独立服务存在的,这个服务叫做 php-fpm什么是 php-fpm pool :也就是 php-fpm 的进程池,这个进程池中运行了多个子进程,用来并发处理 ...

  10. linux下getsockopt和setsockopt详解及测试

    linux下getsockopt和setsockopt详解及测试 NAME 名字 getsockopt, setsockopt - get and set options on sockets 获取或 ...