UIWindow+AHKAdditions.m 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // UIWindow+AHKAdditions.m
  3. // AHKActionSheetExample
  4. //
  5. // Created by Arkadiusz on 14-04-14.
  6. // Copyright (c) 2014 Arkadiusz Holko. All rights reserved.
  7. //
  8. #import "UIWindow+AHKAdditions.h"
  9. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  10. @implementation UIWindow (AHKAdditions)
  11. #pragma mark - Public
  12. - (UIViewController *)ahk_viewControllerForStatusBarStyle
  13. {
  14. UIViewController *currentViewController = [self currentViewController];
  15. while ([currentViewController childViewControllerForStatusBarStyle]) {
  16. currentViewController = [currentViewController childViewControllerForStatusBarStyle];
  17. }
  18. return currentViewController;
  19. }
  20. - (UIViewController *)ahk_viewControllerForStatusBarHidden
  21. {
  22. UIViewController *currentViewController = [self currentViewController];
  23. while ([currentViewController childViewControllerForStatusBarHidden]) {
  24. currentViewController = [currentViewController childViewControllerForStatusBarHidden];
  25. }
  26. return currentViewController;
  27. }
  28. - (UIImage *)ahk_snapshot
  29. {
  30. // source (under MIT license): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35
  31. // UIWindow doesn't have to be rotated on iOS 8+.
  32. BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");
  33. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  34. CGSize imageSize = CGSizeZero;
  35. if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation) {
  36. imageSize = [UIScreen mainScreen].bounds.size;
  37. } else {
  38. imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
  39. }
  40. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  41. CGContextRef context = UIGraphicsGetCurrentContext();
  42. CGContextSaveGState(context);
  43. CGContextTranslateCTM(context, self.center.x, self.center.y);
  44. CGContextConcatCTM(context, self.transform);
  45. CGContextTranslateCTM(context, -self.bounds.size.width * self.layer.anchorPoint.x, -self.bounds.size.height * self.layer.anchorPoint.y);
  46. // correct for the screen orientation
  47. if (!ignoreOrientation) {
  48. if (orientation == UIInterfaceOrientationLandscapeLeft) {
  49. CGContextRotateCTM(context, (CGFloat)M_PI_2);
  50. CGContextTranslateCTM(context, 0, -imageSize.width);
  51. } else if (orientation == UIInterfaceOrientationLandscapeRight) {
  52. CGContextRotateCTM(context, (CGFloat)-M_PI_2);
  53. CGContextTranslateCTM(context, -imageSize.height, 0);
  54. } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
  55. CGContextRotateCTM(context, (CGFloat)M_PI);
  56. CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
  57. }
  58. }
  59. if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
  60. [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
  61. } else {
  62. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  63. }
  64. CGContextRestoreGState(context);
  65. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  66. UIGraphicsEndImageContext();
  67. return image;
  68. }
  69. #pragma mark - Private
  70. - (UIViewController *)currentViewController
  71. {
  72. UIViewController *viewController = self.rootViewController;
  73. while (viewController.presentedViewController) {
  74. viewController = viewController.presentedViewController;
  75. }
  76. return viewController;
  77. }
  78. @end