12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- //
- // UIView+Screenshot.m
- // WildFireChat
- //
- // Created by heavyrain lee on 02/01/2018.
- // Copyright © 2018 WildFireChat. All rights reserved.
- //
- #import "UIView+Screenshot.h"
- @implementation UIView (Screenshot)
- - (UIImage *)screenshot
- {
- return [self screenshotWithRect:self.bounds];
- }
- - (UIImage *)screenshotWithRect:(CGRect)rect;
- {
- UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
-
- CGContextRef context = UIGraphicsGetCurrentContext();
- if (context == NULL)
- {
- return nil;
- }
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
-
- if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
- {
- [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
- }
- else
- {
- [self.layer renderInContext:context];
- }
-
- CGContextRestoreGState(context);
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
- @end
|