123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // UIView+Screenshot.m
- // Created by MacKun on 15/4/25.
- // Copyright (c) 2015年 MacKun All rights reserved.
- //
- #import "UIView+Screenshot.h"
- #import <QuartzCore/QuartzCore.h>
- @implementation UIView (Screenshot)
- /**
- * @brief view截图
- *
- * @return 截图
- */
- - (UIImage *)screenshot {
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
- if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
- {
- [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
- }
- else
- {
- [self.layer renderInContext:UIGraphicsGetCurrentContext()];
- }
-
- UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return screenshot;
- }
- /**
- * @author Jakey
- *
- * @brief 截图一个view中所有视图 包括旋转缩放效果
- *
- * @param aView 一个view
- * @param limitWidth 限制缩放的最大宽度 保持默认传0
- *
- * @return 截图
- */
- - (UIImage *)screenshot:(CGFloat)maxWidth{
- CGAffineTransform oldTransform = self.transform;
- CGAffineTransform scaleTransform = CGAffineTransformIdentity;
-
- // if (!isnan(scale)) {
- // CGAffineTransform transformScale = CGAffineTransformMakeScale(scale, scale);
- // scaleTransform = CGAffineTransformConcat(oldTransform, transformScale);
- // }
- if (!isnan(maxWidth) && maxWidth>0) {
- CGFloat maxScale = maxWidth/CGRectGetWidth(self.frame);
- CGAffineTransform transformScale = CGAffineTransformMakeScale(maxScale, maxScale);
- scaleTransform = CGAffineTransformConcat(oldTransform, transformScale);
-
- }
- if(!CGAffineTransformEqualToTransform(scaleTransform, CGAffineTransformIdentity)){
- self.transform = scaleTransform;
- }
-
- CGRect actureFrame = self.frame; //已经变换过后的frame
- CGRect actureBounds= self.bounds;//CGRectApplyAffineTransform();
-
- //begin
- UIGraphicsBeginImageContextWithOptions(actureFrame.size, NO, 0.0);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSaveGState(context);
- // CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
- CGContextTranslateCTM(context,actureFrame.size.width/2, actureFrame.size.height/2);
- CGContextConcatCTM(context, self.transform);
- CGPoint anchorPoint = self.layer.anchorPoint;
- CGContextTranslateCTM(context,
- -actureBounds.size.width * anchorPoint.x,
- -actureBounds.size.height * anchorPoint.y);
- if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
- {
- [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
- }
- else
- {
- [self.layer renderInContext:UIGraphicsGetCurrentContext()];
- }
- UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- //end
- self.transform = oldTransform;
-
- return screenshot;
- }
- @end
|