12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- //
- // UIView+ScreenShot.m
- // shiku_im
- //
- // Created by Apple on 16/12/7.
- // Copyright © 2016年 Reese. 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);
-
- //[self layoutIfNeeded];
-
- if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
- {
- [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
- }
- else
- {
- [self.layer renderInContext:context];
- }
-
- CGContextRestoreGState(context);
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- // NSData *imageData = UIImageJPEGRepresentation(image, 1); // convert to jpeg
- // image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];
-
- return image;
- }
- - (UIImage *)snapshot:(UIView *)view
- {
- UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
- [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return image;
- }
- - (UIImage *)viewSnapshot:(UIView *)view withInRect:(CGRect)rect
- {
- // UIGraphicsBeginImageContext(view.bounds.size);
- UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO,[UIScreen mainScreen].scale);
- [view.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- // image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage,rect)];
- return image;
- }
- + (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
- {
- // UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize), NO,[UIScreen mainScreen].scale);
- [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
- UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return scaledImage;
-
- }
- @end
|