ZCUtils.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // ZCUtils.m
  3. // XLChat
  4. //
  5. // Created by 张灿 on 2017/11/10.
  6. // Copyright © 2017年 张灿. All rights reserved.
  7. //
  8. #import "ZCUtils.h"
  9. @implementation ZCUtils
  10. +(NSString *)getHHMMSSFromSS:(NSInteger)seconds{
  11. //format of hour
  12. NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
  13. //format of minute
  14. NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
  15. //format of second
  16. NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
  17. //format of time
  18. NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
  19. return format_time;
  20. }
  21. +(NSString *)getMMSSFromSS:(NSInteger)seconds{
  22. //format of minute
  23. NSString *str_minute = [NSString stringWithFormat:@"%02d",seconds/60];
  24. //format of second
  25. NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
  26. //format of time
  27. NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
  28. return format_time;
  29. }
  30. +(NSString *)getTimeStrFromSS:(NSInteger)seconds{
  31. //format of hour
  32. NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
  33. //format of minute
  34. NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
  35. //format of second
  36. NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
  37. //format of time
  38. NSString *format_time = @"";
  39. if (seconds/3600>0) {
  40. format_time = [NSString stringWithFormat:@"%@%@时",format_time,str_hour];
  41. }
  42. if ((seconds%3600)/60>0) {
  43. format_time = [NSString stringWithFormat:@"%@%@分",format_time,str_minute];
  44. }
  45. if (seconds%60>0) {
  46. format_time = [NSString stringWithFormat:@"%@%@秒",format_time,str_second];
  47. }
  48. return format_time;
  49. }
  50. +(NSString *)getMinTimeStrFromSS:(NSInteger)seconds{
  51. //format of minute
  52. NSString *str_minute = [NSString stringWithFormat:@"%02d",seconds/60];
  53. //format of second
  54. NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
  55. //format of time
  56. NSString *format_time = @"";
  57. if (seconds/60>0) {
  58. format_time = [NSString stringWithFormat:@"%@%@分",format_time,str_minute];
  59. }
  60. if (seconds%60>0) {
  61. format_time = [NSString stringWithFormat:@"%@%@秒",format_time,str_second];
  62. }
  63. return format_time;
  64. }
  65. + (UIViewController *)getCurrentVC {
  66. // UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
  67. // if (!window) {
  68. // return nil;
  69. // }
  70. // UIView *tempView;
  71. // for (UIView *subview in window.subviews) {
  72. // if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
  73. // tempView = subview;
  74. // break;
  75. // }
  76. // }
  77. // if (!tempView) {
  78. // tempView = [window.subviews lastObject];
  79. // }
  80. //
  81. // id nextResponder = [tempView nextResponder];
  82. // while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
  83. // tempView = [tempView.subviews firstObject];
  84. //
  85. // if (!tempView) {
  86. // return nil;
  87. // }
  88. // nextResponder = [tempView nextResponder];
  89. // }
  90. // return (UIViewController *)nextResponder;
  91. UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  92. UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
  93. return currentVC;
  94. }
  95. + (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
  96. {
  97. UIViewController *currentVC;
  98. if ([rootVC presentedViewController]) {
  99. // 视图是被presented出来的
  100. rootVC = [rootVC presentedViewController];
  101. }
  102. if ([rootVC isKindOfClass:[UITabBarController class]]) {
  103. // 根视图为UITabBarController
  104. currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
  105. } else if ([rootVC isKindOfClass:[UINavigationController class]]){
  106. // 根视图为UINavigationController
  107. currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
  108. } else {
  109. // 根视图为非导航类
  110. currentVC = rootVC;
  111. }
  112. return currentVC;
  113. }
  114. + (UIImage *)getImage:(UIView *)shareView
  115. {
  116. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  117. if (CGSizeEqualToSize(shareView.frame.size, CGSizeZero)) {
  118. return nil;
  119. }
  120. UIGraphicsBeginImageContextWithOptions(CGSizeMake(shareView.frame.size.width,shareView.frame.size.height ), NO, 0.0);
  121. [shareView.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
  122. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
  123. UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
  124. UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
  125. return viewImage;
  126. }
  127. + (void)clipCorner:(UIRectCorner)corner View:(UIView*)view size:(CGSize)size {
  128. UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:size]
  129. ;
  130. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  131. maskLayer.frame = view.bounds;
  132. maskLayer.path = maskPath.CGPath;
  133. view.layer.mask = maskLayer;
  134. }
  135. @end