UIView+Screenshot.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // UIView+Screenshot.m
  3. // Created by MacKun on 15/4/25.
  4. // Copyright (c) 2015年 MacKun All rights reserved.
  5. //
  6. #import "UIView+Screenshot.h"
  7. #import <QuartzCore/QuartzCore.h>
  8. @implementation UIView (Screenshot)
  9. /**
  10. * @brief view截图
  11. *
  12. * @return 截图
  13. */
  14. - (UIImage *)screenshot {
  15. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
  16. if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
  17. {
  18. [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
  19. }
  20. else
  21. {
  22. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  23. }
  24. UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
  25. UIGraphicsEndImageContext();
  26. return screenshot;
  27. }
  28. /**
  29. * @author Jakey
  30. *
  31. * @brief 截图一个view中所有视图 包括旋转缩放效果
  32. *
  33. * @param aView 一个view
  34. * @param limitWidth 限制缩放的最大宽度 保持默认传0
  35. *
  36. * @return 截图
  37. */
  38. - (UIImage *)screenshot:(CGFloat)maxWidth{
  39. CGAffineTransform oldTransform = self.transform;
  40. CGAffineTransform scaleTransform = CGAffineTransformIdentity;
  41. // if (!isnan(scale)) {
  42. // CGAffineTransform transformScale = CGAffineTransformMakeScale(scale, scale);
  43. // scaleTransform = CGAffineTransformConcat(oldTransform, transformScale);
  44. // }
  45. if (!isnan(maxWidth) && maxWidth>0) {
  46. CGFloat maxScale = maxWidth/CGRectGetWidth(self.frame);
  47. CGAffineTransform transformScale = CGAffineTransformMakeScale(maxScale, maxScale);
  48. scaleTransform = CGAffineTransformConcat(oldTransform, transformScale);
  49. }
  50. if(!CGAffineTransformEqualToTransform(scaleTransform, CGAffineTransformIdentity)){
  51. self.transform = scaleTransform;
  52. }
  53. CGRect actureFrame = self.frame; //已经变换过后的frame
  54. CGRect actureBounds= self.bounds;//CGRectApplyAffineTransform();
  55. //begin
  56. UIGraphicsBeginImageContextWithOptions(actureFrame.size, NO, 0.0);
  57. CGContextRef context = UIGraphicsGetCurrentContext();
  58. CGContextSaveGState(context);
  59. // CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
  60. CGContextTranslateCTM(context,actureFrame.size.width/2, actureFrame.size.height/2);
  61. CGContextConcatCTM(context, self.transform);
  62. CGPoint anchorPoint = self.layer.anchorPoint;
  63. CGContextTranslateCTM(context,
  64. -actureBounds.size.width * anchorPoint.x,
  65. -actureBounds.size.height * anchorPoint.y);
  66. if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
  67. {
  68. [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
  69. }
  70. else
  71. {
  72. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  73. }
  74. UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
  75. UIGraphicsEndImageContext();
  76. //end
  77. self.transform = oldTransform;
  78. return screenshot;
  79. }
  80. @end