Skip to content

Instantly share code, notes, and snippets.

@buoge
Created April 24, 2017 09:47
Show Gist options
  • Save buoge/a94798cd49854a569fdc32825c015167 to your computer and use it in GitHub Desktop.
Save buoge/a94798cd49854a569fdc32825c015167 to your computer and use it in GitHub Desktop.
截屏并展示在屏幕上
// 调用方法 didFinishLaunchingWithOptions中调用
// 注册截屏事件通知: AppScreenshotDelegate(appDelegate: self).registObserver()
import Foundation
class AppScreenshotDelegate: NSObject {
weak var mAppDelegate: AppDelegate?
init(appDelegate: AppDelegate?) {
self.mAppDelegate = appDelegate
}
func registObserver(){
if let _ = mAppDelegate {
let mainQueue = OperationQueue.main
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot,object: nil,queue: mainQueue) { notification in
// self 如果使用弱引用,代码执行到闭包这里就为nil了,被释放了
self.takeScreenshot()
print("registObserver 用户产生截图事件......")
}
}
}
// - (void)userDidTakeScreenshot:(NSNotification *)notification
// {
// NSLog(@"检测到截屏");
//
// //人为截屏, 模拟用户截屏行为, 获取所截图片
// UIImage *image_ = [self imageWithScreenshot];
//
// //添加显示
// UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
// imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2);
//
// //添加边框
// CALayer * layer = [imgvPhoto layer];
// layer.borderColor = [
// [UIColor whiteColor] CGColor];
// layer.borderWidth = 5.0f;
// //添加四个边阴影
// imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
// imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);
// imgvPhoto.layer.shadowOpacity = 0.5;
// imgvPhoto.layer.shadowRadius = 10.0;
// //添加两个边阴影
// imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
// imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4);
// imgvPhoto.layer.shadowOpacity = 0.5;
// imgvPhoto.layer.shadowRadius = 2.0;
//
// [self.window addSubview:imgvPhoto];
// }
func takeScreenshot(){
print("takeScreenshot 用户产生截图事件......")
let mImage = imageWithScreenshot()
// UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
// imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2);
//添加显示
let imgvPhoto = UIImageView(image: mImage)
let screenSize = mAppDelegate?.window?.frame.size
if let _screenSize = screenSize {
//CGRect(x: x, y: 0, width: width, height: height)
imgvPhoto.frame = CGRect(x: _screenSize.width/2, y: _screenSize.height/2, width: _screenSize.width/2, height: _screenSize.height/2)
}
// 添加边框,好区分
imgvPhoto.layer.borderColor = UIColor.blue.cgColor
imgvPhoto.layer.borderWidth = 5.0
mAppDelegate?.window?.addSubview(imgvPhoto)
}
// /**
// * 返回截取到的图片
// *
// * @return UIImage *
// */
// - (UIImage *)imageWithScreenshot
// {
// NSData *imageData = [self dataWithScreenshotInPNGFormat];
// return [UIImage imageWithData:imageData];
// }
func imageWithScreenshot() -> UIImage? {
let imageData = dataWithScreenshotInPNGFormat()
if let _imageData = imageData {
return UIImage(data: _imageData)
}
return nil
}
// /**
// * 截取当前屏幕
// *
// * @return NSData *
// */
// - (NSData *)dataWithScreenshotInPNGFormat
// {
/**
* 截取当前屏幕
* @return NSData *
*/
func dataWithScreenshotInPNGFormat() -> Data? {
// CGSize imageSize = CGSizeZero;
var imageSize = CGSize.zero;
// 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);
//
let orientation = UIApplication.shared.statusBarOrientation
let screenSize = UIScreen.main.bounds.size
if (UIInterfaceOrientationIsPortrait(orientation)) {
// 竖屏
imageSize = screenSize
} else {
// 横屏
imageSize = CGSize(width: screenSize.height,height: screenSize.width)
}
// UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
// CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
// for (UIWindow *window in [[UIApplication sharedApplication] windows])
// {
for subWindow in (UIApplication.shared.windows) {
// CGContextSaveGState(context);
context.saveGState()
// CGContextTranslateCTM(context, window.center.x, window.center.y);
context.translateBy(x: subWindow.center.x, y: subWindow.center.y)
// CGContextConcatCTM(context, window.transform);
context.concatenate(subWindow.transform)
// CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
context.translateBy(x: -subWindow.bounds.size.width * subWindow.layer.anchorPoint.x,
y: -subWindow.bounds.size.height * subWindow.layer.anchorPoint.y)
// if (orientation == UIInterfaceOrientationLandscapeLeft)
// {
// CGContextRotateCTM(context, M_PI_2);
// CGContextTranslateCTM(context, 0, -imageSize.width);
// }
if (orientation == UIInterfaceOrientation.landscapeLeft) {
context.rotate(by: CGFloat(M_PI_2))
context.translateBy(x: 0, y: -imageSize.width)
}
// else if (orientation == UIInterfaceOrientationLandscapeRight)
// {
// CGContextRotateCTM(context, -M_PI_2);
// CGContextTranslateCTM(context, -imageSize.height, 0);
// }
else if (orientation == UIInterfaceOrientation.landscapeRight) {
context.rotate(by: -CGFloat(M_PI_2))
context.translateBy(x: -imageSize.width, y: 0)
}
// else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
// CGContextRotateCTM(context, M_PI);
// CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
// }
else if (orientation == UIInterfaceOrientation.portraitUpsideDown) {
context.rotate(by: CGFloat(M_PI))
context.translateBy(x: -imageSize.width, y: -imageSize.height)
}
// if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
// {
// [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
// }
// else
// {
// [window.layer renderInContext:context];
// }
// CGContextRestoreGState(context);
// }
//drawHierarchy(in:afterScreenUpdates:)
if ( subWindow.responds(to: #selector(UIWindow.drawHierarchy(in:afterScreenUpdates:))) ){
subWindow.drawHierarchy(in: subWindow.bounds, afterScreenUpdates: true)
} else {
subWindow.layer.render(in: context)
}
context.restoreGState()
}
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if let _image = image {
return UIImagePNGRepresentation(_image)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment