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. jquery ajax 请求中 中文汉字的 转码问题

    1.汉字参数直接跟在请求连接的后面,这样需要使用encodeURIComponent(fileName)或者encodeURI(fileName)转码两次 后台使用URLDecoder.decode( ...

  2. 【转】 Android定时器

    转载自:http://www.android-study.com/pingtaikaifa/508.html 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sl ...

  3. getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)

    今天捣鼓了一下午.getActionBar().setDisplayHomeAsUpEnabled(true)总是报空指针.在我的还有一个Android4.4.2的项目中就没有一点问题.我还以为是我自 ...

  4. iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  5. 怎么打乱List中元素的顺序

    使用Collections类中shuffle随机打乱List内部元素顺序 原文地址:http://blog.csdn.net/warren2013/article/details/17414771 / ...

  6. 如何构建日均千万PV Web站点 (一)

    其实大多数互联网网站起初的网站架构都是(Linux+Apache+MySQL+PHP). 不过随着时代的发展,科技的进步.互联网进入寻常百姓家的生活.所谓的用户的需求,铸就了一个个互联网大牛: htt ...

  7. eclipse cdt运行c程序报错“launch failed,binary not found”

    1. 安装了eclipsecdt版 2. 采用mingw 编译第一个c程序,报错“launch failed,binary not found”.检查是mingw下的bin目录在环境变量里设置错了. ...

  8. 十款不错的Hybrid App移动开发框架

    本文转载至http://www.pureasme.com/blog/2015/0419476.html ionic 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应 ...

  9. U盘安装centos7:不能载入到安装界面

    在用U盘安装centos7时,我们需要修改镜像位置: 选择第一项:Install CentOS 7 ,按 e(也有可能是tab键)键进入编辑界面. 将 vmlinuz initrd=initrd.im ...

  10. 【调试错误】找不到 System.Web.Mvc 引用

    今天调试新项目,从svn上clone下来,发现运行不了. System.Web.Mvc 显示黄色叹号 解决方案如下: 安装一个 MVC 的插件.AspNetMVC2_VS2008.exe 下载地址:h ...