本文只包括最基本的iOS截屏代码,不包括处理横屏的屏幕旋转情况。如果想简单使用,可以直接拷走使用。
#import "UIView+DESSUtil.h"
@implementation UIView (DESSUtil)
- (UIImage *)ss_captureScreenshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
// IOS7及其后续版本
if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector:
@selector(drawViewHierarchyInRect:afterScreenUpdates:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)];
CGRect arg2 = self.bounds;
BOOL arg3 = YES;
[invocation setArgument:&arg2 atIndex:2];
[invocation setArgument:&arg3 atIndex:3];
[invocation invoke];
} else { // IOS7之前的版本
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
- (UIImage *)ss_captureScreenshotWithStatusBar{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
// IOS7及其后续版本
if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector:
@selector(drawViewHierarchyInRect:afterScreenUpdates:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)];
CGRect arg2 = self.bounds;
BOOL arg3 = YES;
[invocation setArgument:&arg2 atIndex:2];
[invocation setArgument:&arg3 atIndex:3];
[invocation invoke];
} else { // IOS7之前的版本
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//2、状态栏
UIImage *statusImg;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
if (@available(iOS 13.0, *)) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
UIView *_statusBar = nil;
if ([statusBarManager respondsToSelector:@selector(createLocalStatusBar)]) {
UIView *_localStatusBar = [statusBarManager performSelector:@selector(createLocalStatusBar)];
if ([_localStatusBar respondsToSelector:@selector(statusBar)]) {
_statusBar = [_localStatusBar performSelector:@selector(statusBar)];
UIGraphicsBeginImageContextWithOptions(_statusBar.size, NO, [UIScreen mainScreen].scale);
[_statusBar.layer renderInContext:UIGraphicsGetCurrentContext()];
statusImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
#pragma clang diagnostic pop
} else {
// Fallback on earlier versions
}
}else{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
UIGraphicsBeginImageContextWithOptions(statusBar.size, NO, [UIScreen mainScreen].scale);
[statusBar.layer renderInContext:UIGraphicsGetCurrentContext()];
statusImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
//合并
UIGraphicsBeginImageContext(screenshot.size);
[screenshot drawInRect:CGRectMake(0, 0, screenshot.size.width, screenshot.size.height)];
[statusImg drawInRect:CGRectMake(0, 0, statusImg.size.width, statusImg.size.height)];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshotImage;
}
@end