Created
April 8, 2015 03:14
-
-
Save hechen/62121542b1701bdf71c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+Image.h | |
// ViewImage | |
// | |
// Created by hechen on 15/4/8. | |
// Copyright (c) 2015年 hechen. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIView(Image) | |
- (UIImage *)image; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+Image.m | |
// ViewImage | |
// | |
// Created by hechen on 15/4/8. | |
// Copyright (c) 2015年 hechen. All rights reserved. | |
// | |
#import "UIView+Image.h" | |
@implementation UIView(Image) | |
- (UIImage *)image{ | |
CGSize imageSize = [self bounds].size; | |
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// Iterate over every window from back to front | |
for (UIWindow *window in [[UIApplication sharedApplication] windows]){ | |
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]){ | |
// -renderInContext: renders in the coordinate space of the layer, | |
// so we must first apply the layer's geometry to the graphics context | |
CGContextSaveGState(context); | |
// Center the context around the window's anchor point | |
CGContextTranslateCTM(context, [window center].x, [window center].y); | |
// Apply the window's transform about the anchor point | |
CGContextConcatCTM(context, [window transform]); | |
// Offset by the portion of the bounds left of and above the anchor point | |
CGContextTranslateCTM(context, -[window bounds].size.width *[[window layer] anchorPoint].x, -[window bounds].size.height *[[window layer] anchorPoint].y); | |
// Render the layer hierarchy to the current context | |
[[window layer] renderInContext:context]; | |
// Restore the context | |
CGContextRestoreGState(context); | |
} | |
} | |
// Retrieve the screenshot image | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment