[关闭]
@buoge 2017-08-07T11:06:41.000000Z 字数 3675 阅读 1779

iOS 截屏分享(包含状态栏与不包含状态栏)

iOS


iOS8以上的新方法PhotoKit

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

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

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

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

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

2. OTScreenShotHelper.h 中暴露mergeStatusBarToContext方法

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

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

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

  1. for subWindow in (UIApplication.shared.windows) {
  2. context.saveGState()
  3. context.translateBy(x: subWindow.center.x, y: subWindow.center.y)
  4. context.concatenate(subWindow.transform)
  5. context.translateBy(x: -subWindow.bounds.size.width * subWindow.layer.anchorPoint.x,
  6. y: -subWindow.bounds.size.height * subWindow.layer.anchorPoint.y)
  7. if (orientation == UIInterfaceOrientation.landscapeLeft) {
  8. context.rotate(by: CGFloat(M_PI_2))
  9. context.translateBy(x: 0, y: -imageSize.width)
  10. }
  11. else if (orientation == UIInterfaceOrientation.landscapeRight) {
  12. context.rotate(by: -CGFloat(M_PI_2))
  13. context.translateBy(x: -imageSize.width, y: 0)
  14. }
  15. else if (orientation == UIInterfaceOrientation.portraitUpsideDown) {
  16. context.rotate(by: CGFloat(M_PI))
  17. context.translateBy(x: -imageSize.width, y: -imageSize.height)
  18. }
  19. if ( subWindow.responds(to: #selector(UIWindow.drawHierarchy(in:afterScreenUpdates:))) ){
  20. subWindow.drawHierarchy(in: subWindow.bounds, afterScreenUpdates: true)
  21. } else {
  22. subWindow.layer.render(in: context)
  23. }
  24. context.restoreGState()
  25. // 这部分是截图带有statusbar的关键,在这里把状态栏截图动态添加到了原有图片中,在未带状态栏截图代码如下位置添加如下代码即可
  26. let statusBarRect = CGRect(x: 0, y: 0, width: AppWidth, height: 20)
  27. OTScreenshotHelper.mergeStatusBar(to: context, rect: statusBarRect, screenshotOrientation: orientation)
  28. // End
  29. }
  30. let image = UIGraphicsGetImageFromCurrentImageContext();
  31. UIGraphicsEndImageContext();
  32. return image

未带状态栏截图代码参见:
https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167


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

image_1blue6gp9u7d1svtr38hug1m9im.png-119.7kB
* swift版截图并展示
https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167
* 这个代码有个问题在播放视频的页面播放,截取出来是黑屏

  1. 创建两个UIImage
  2. UIImage *image1 = [UIImage imageNamed:@"iOSDevTip"];
  3. UIImage *image2 = [UIImage imageNamed:@"CodePush"];
  4. 创建UIImage的方法有很多种,我们就简单的通过imageNamed:方法来创建。
  5. 合并之后的size
  6. CGSize size = CGSizeMake(image1.size.width + image2.size.width, image1.size.height);
  7. 合并两个UIImage,需要计算合并之后的size。假设这两个UIImage的高度是是相同的,把他们的宽度相加,得到合并之后的UIImagesize
  8. 合并方法有了UIImagesize接下来就是把两个UIImage合并,方法如下:
  9. UIGraphicsBeginImageContext(size);
  10. [image1 drawInRect:CGRectMake(0, 0, image1.size.width, size.height)];
  11. [image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, size.height)];
  12. UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
  13. UIGraphicsEndImageContext();
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注