123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- //
- // YBIBUtilities.m
- // YBImageBrowserDemo
- //
- // Created by 杨少 on 2018/4/11.
- // Copyright © 2018年 波儿菜. All rights reserved.
- //
- #import "YBIBUtilities.h"
- #import <sys/utsname.h>
- UIWindow * _Nullable YBIBNormalWindow(void) {
- UIWindow *window = [[UIApplication sharedApplication] keyWindow];
- if (window.windowLevel != UIWindowLevelNormal) {
- NSArray *windows = [[UIApplication sharedApplication] windows];
- for(UIWindow *temp in windows) {
- if (temp.windowLevel == UIWindowLevelNormal) {
- return temp;
- }
- }
- }
- return window;
- }
- UIViewController * _Nullable YBIBTopController(void) {
- return YBIBTopControllerByWindow(YBIBNormalWindow());
- }
- UIViewController * _Nullable YBIBTopControllerByWindow(UIWindow *window) {
- if (!window) return nil;
-
- UIViewController *top = nil;
- id nextResponder;
- if (window.subviews.count > 0) {
- UIView *frontView = [window.subviews objectAtIndex:0];
- nextResponder = frontView.nextResponder;
- }
- if (nextResponder && [nextResponder isKindOfClass:UIViewController.class]) {
- top = nextResponder;
- } else {
- top = window.rootViewController;
- }
-
- while ([top isKindOfClass:UITabBarController.class] || [top isKindOfClass:UINavigationController.class] || top.presentedViewController) {
- if ([top isKindOfClass:UITabBarController.class]) {
- top = ((UITabBarController *)top).selectedViewController;
- } else if ([top isKindOfClass:UINavigationController.class]) {
- top = ((UINavigationController *)top).topViewController;
- } else if (top.presentedViewController) {
- top = top.presentedViewController;
- }
- }
- return top;
- }
- BOOL YBIBLowMemory(void) {
- static BOOL lowMemory = NO;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- unsigned long long physicalMemory = [[NSProcessInfo processInfo] physicalMemory];
- lowMemory = physicalMemory > 0 && physicalMemory < 1024 * 1024 * 1500;
- });
- return lowMemory;
- }
- BOOL YBIBIsIphoneXSeries(void) {
- return YBIBStatusbarHeight() > 20;
- }
- CGFloat YBIBStatusbarHeight(void) {
- CGFloat height = 0;
- if (@available(iOS 11.0, *)) {
- height = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top;
- }
- if (height <= 0) {
- height = UIApplication.sharedApplication.statusBarFrame.size.height;
- }
- if (height <= 0) {
- height = 20;
- }
- return height;
- }
- CGFloat YBIBSafeAreaBottomHeight(void) {
- CGFloat bottom = 0;
- if (@available(iOS 11.0, *)) {
- bottom = UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
- }
- return bottom;
- }
- UIImage *YBIBSnapshotView(UIView *view) {
- UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
- format.opaque = YES;
- format.scale = [UIScreen mainScreen].scale;
- UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size format:format];
- UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
- [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
- }];
- return image;
- }
- UIEdgeInsets YBIBPaddingByBrowserOrientation(UIDeviceOrientation orientation) {
- UIEdgeInsets padding = UIEdgeInsetsZero;
- if (!YBIBIsIphoneXSeries()) return padding;
-
- UIDeviceOrientation barOrientation = (UIDeviceOrientation)UIApplication.sharedApplication.statusBarOrientation;
-
- if (UIDeviceOrientationIsLandscape(orientation)) {
- BOOL same = orientation == barOrientation;
- BOOL reverse = !same && UIDeviceOrientationIsLandscape(barOrientation);
- if (same) {
- padding.bottom = YBIBSafeAreaBottomHeight();
- padding.top = 0;
- } else if (reverse) {
- padding.top = YBIBSafeAreaBottomHeight();
- padding.bottom = 0;
- }
- padding.left = padding.right = MAX(YBIBSafeAreaBottomHeight(), YBIBStatusbarHeight());
- } else {
- if (orientation == UIDeviceOrientationPortrait) {
- padding.top = YBIBStatusbarHeight();
- padding.bottom = barOrientation == UIDeviceOrientationPortrait ? YBIBSafeAreaBottomHeight() : 0;
- } else {
- padding.bottom = YBIBStatusbarHeight();
- padding.top = barOrientation == UIDeviceOrientationPortrait ? YBIBSafeAreaBottomHeight() : 0;
- }
- padding.left = padding.right = UIDeviceOrientationIsLandscape(barOrientation) ? YBIBSafeAreaBottomHeight() : 0 ;
- }
- return padding;
- }
- @implementation YBIBUtilities
- @end
|