UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if(window == screenWindow)
{
break;
}else{
[window.layer renderInContext:context];
}
}
if ([screenWindow respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[screenWindow drawViewHierarchyInRect:screenWindow.bounds afterScreenUpdates:YES];
} else {
[screenWindow.layer renderInContext:context];
}
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
screenWindow.layer.contents = nil;
UIGraphicsEndImageContext();
float iOSVersion = [UIDevice currentDevice].systemVersion.floatValue;
if(iOSVersion < 8.0)
{
image = [self rotateUIInterfaceOrientationImage:image];
}
-(UIImage *)rotateUIInterfaceOrientationImage:(UIImage *)image{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationLandscapeRight:
{
LOG(@"右");
image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationLeft];
}
break;
case UIInterfaceOrientationLandscapeLeft:
{
LOG(@"左");
image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationRight];
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
{
LOG(@"上");
image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationDown];
}
break;
case UIInterfaceOrientationPortrait:
{
LOG(@"下");
image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationUp];
}
break;
case UIInterfaceOrientationUnknown:
{
LOG(@"不知道");
}
break;
default:
break;
}
return image;
}