YBIBUtilities.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // YBIBUtilities.m
  3. // YBImageBrowserDemo
  4. //
  5. // Created by 杨少 on 2018/4/11.
  6. // Copyright © 2018年 波儿菜. All rights reserved.
  7. //
  8. #import "YBIBUtilities.h"
  9. #import <sys/utsname.h>
  10. UIWindow * _Nullable YBIBNormalWindow(void) {
  11. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  12. if (window.windowLevel != UIWindowLevelNormal) {
  13. NSArray *windows = [[UIApplication sharedApplication] windows];
  14. for(UIWindow *temp in windows) {
  15. if (temp.windowLevel == UIWindowLevelNormal) {
  16. return temp;
  17. }
  18. }
  19. }
  20. return window;
  21. }
  22. UIViewController * _Nullable YBIBTopController(void) {
  23. return YBIBTopControllerByWindow(YBIBNormalWindow());
  24. }
  25. UIViewController * _Nullable YBIBTopControllerByWindow(UIWindow *window) {
  26. if (!window) return nil;
  27. UIViewController *top = nil;
  28. id nextResponder;
  29. if (window.subviews.count > 0) {
  30. UIView *frontView = [window.subviews objectAtIndex:0];
  31. nextResponder = frontView.nextResponder;
  32. }
  33. if (nextResponder && [nextResponder isKindOfClass:UIViewController.class]) {
  34. top = nextResponder;
  35. } else {
  36. top = window.rootViewController;
  37. }
  38. while ([top isKindOfClass:UITabBarController.class] || [top isKindOfClass:UINavigationController.class] || top.presentedViewController) {
  39. if ([top isKindOfClass:UITabBarController.class]) {
  40. top = ((UITabBarController *)top).selectedViewController;
  41. } else if ([top isKindOfClass:UINavigationController.class]) {
  42. top = ((UINavigationController *)top).topViewController;
  43. } else if (top.presentedViewController) {
  44. top = top.presentedViewController;
  45. }
  46. }
  47. return top;
  48. }
  49. BOOL YBIBLowMemory(void) {
  50. static BOOL lowMemory = NO;
  51. static dispatch_once_t onceToken;
  52. dispatch_once(&onceToken, ^{
  53. unsigned long long physicalMemory = [[NSProcessInfo processInfo] physicalMemory];
  54. lowMemory = physicalMemory > 0 && physicalMemory < 1024 * 1024 * 1500;
  55. });
  56. return lowMemory;
  57. }
  58. BOOL YBIBIsIphoneXSeries(void) {
  59. return YBIBStatusbarHeight() > 20;
  60. }
  61. CGFloat YBIBStatusbarHeight(void) {
  62. CGFloat height = 0;
  63. if (@available(iOS 11.0, *)) {
  64. height = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top;
  65. }
  66. if (height <= 0) {
  67. height = UIApplication.sharedApplication.statusBarFrame.size.height;
  68. }
  69. if (height <= 0) {
  70. height = 20;
  71. }
  72. return height;
  73. }
  74. CGFloat YBIBSafeAreaBottomHeight(void) {
  75. CGFloat bottom = 0;
  76. if (@available(iOS 11.0, *)) {
  77. bottom = UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
  78. }
  79. return bottom;
  80. }
  81. UIImage *YBIBSnapshotView(UIView *view) {
  82. UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
  83. format.opaque = YES;
  84. format.scale = [UIScreen mainScreen].scale;
  85. UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size format:format];
  86. UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
  87. [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
  88. }];
  89. return image;
  90. }
  91. UIEdgeInsets YBIBPaddingByBrowserOrientation(UIDeviceOrientation orientation) {
  92. UIEdgeInsets padding = UIEdgeInsetsZero;
  93. if (!YBIBIsIphoneXSeries()) return padding;
  94. UIDeviceOrientation barOrientation = (UIDeviceOrientation)UIApplication.sharedApplication.statusBarOrientation;
  95. if (UIDeviceOrientationIsLandscape(orientation)) {
  96. BOOL same = orientation == barOrientation;
  97. BOOL reverse = !same && UIDeviceOrientationIsLandscape(barOrientation);
  98. if (same) {
  99. padding.bottom = YBIBSafeAreaBottomHeight();
  100. padding.top = 0;
  101. } else if (reverse) {
  102. padding.top = YBIBSafeAreaBottomHeight();
  103. padding.bottom = 0;
  104. }
  105. padding.left = padding.right = MAX(YBIBSafeAreaBottomHeight(), YBIBStatusbarHeight());
  106. } else {
  107. if (orientation == UIDeviceOrientationPortrait) {
  108. padding.top = YBIBStatusbarHeight();
  109. padding.bottom = barOrientation == UIDeviceOrientationPortrait ? YBIBSafeAreaBottomHeight() : 0;
  110. } else {
  111. padding.bottom = YBIBStatusbarHeight();
  112. padding.top = barOrientation == UIDeviceOrientationPortrait ? YBIBSafeAreaBottomHeight() : 0;
  113. }
  114. padding.left = padding.right = UIDeviceOrientationIsLandscape(barOrientation) ? YBIBSafeAreaBottomHeight() : 0 ;
  115. }
  116. return padding;
  117. }
  118. @implementation YBIBUtilities
  119. @end