iOS屏幕截图

//
//  STTools.h
//  STWebViewController
//
//  Created by linxiaolong on 15/5/11.
//  Copyright (c) 2015年 linxiaolong. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface STTools : NSObject

+ (UIImage *)getScreenshot;

@end

//
//  STTools.m
//  STWebViewController
//
//  Created by linxiaolong on 15/5/11.
//  Copyright (c) 2015年 linxiaolong. All rights reserved.
//

#import "STTools.h"

@implementation STTools

#pragma -mark get screen shot

+ (UIImage *)getScreenshot {
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);
            
            [[window layer] renderInContext:context];
            CGContextRestoreGState(context);
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end

posted @ 2015-08-12 17:19  【se7en】  阅读(296)  评论(0)    收藏  举报