iOS 截屏

来源:互联网 发布:水利水电预算软件 编辑:程序博客网 时间:2024/05/22 00:11
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0); // no ritinaCGContextRef 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;}

0 0