123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- //
- // ZCUtils.m
- // XLChat
- //
- // Created by 张灿 on 2017/11/10.
- // Copyright © 2017年 张灿. All rights reserved.
- //
- #import "ZCUtils.h"
- @implementation ZCUtils
- +(NSString *)getHHMMSSFromSS:(NSInteger)seconds{
- //format of hour
- NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
- //format of minute
- NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
- //format of second
- NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
- //format of time
- NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
- return format_time;
- }
- +(NSString *)getMMSSFromSS:(NSInteger)seconds{
- //format of minute
- NSString *str_minute = [NSString stringWithFormat:@"%02d",seconds/60];
- //format of second
- NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
- //format of time
- NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
- return format_time;
- }
- +(NSString *)getTimeStrFromSS:(NSInteger)seconds{
- //format of hour
- NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
- //format of minute
- NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
- //format of second
- NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
- //format of time
- NSString *format_time = @"";
- if (seconds/3600>0) {
- format_time = [NSString stringWithFormat:@"%@%@时",format_time,str_hour];
- }
- if ((seconds%3600)/60>0) {
- format_time = [NSString stringWithFormat:@"%@%@分",format_time,str_minute];
- }
- if (seconds%60>0) {
- format_time = [NSString stringWithFormat:@"%@%@秒",format_time,str_second];
- }
- return format_time;
- }
- +(NSString *)getMinTimeStrFromSS:(NSInteger)seconds{
- //format of minute
- NSString *str_minute = [NSString stringWithFormat:@"%02d",seconds/60];
- //format of second
- NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
- //format of time
- NSString *format_time = @"";
- if (seconds/60>0) {
- format_time = [NSString stringWithFormat:@"%@%@分",format_time,str_minute];
- }
- if (seconds%60>0) {
- format_time = [NSString stringWithFormat:@"%@%@秒",format_time,str_second];
- }
- return format_time;
- }
- + (UIViewController *)getCurrentVC {
- // UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
- // if (!window) {
- // return nil;
- // }
- // UIView *tempView;
- // for (UIView *subview in window.subviews) {
- // if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
- // tempView = subview;
- // break;
- // }
- // }
- // if (!tempView) {
- // tempView = [window.subviews lastObject];
- // }
- //
- // id nextResponder = [tempView nextResponder];
- // while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
- // tempView = [tempView.subviews firstObject];
- //
- // if (!tempView) {
- // return nil;
- // }
- // nextResponder = [tempView nextResponder];
- // }
- // return (UIViewController *)nextResponder;
-
- UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
- UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
- return currentVC;
-
- }
- + (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
- {
- UIViewController *currentVC;
-
- if ([rootVC presentedViewController]) {
- // 视图是被presented出来的
-
- rootVC = [rootVC presentedViewController];
- }
-
- if ([rootVC isKindOfClass:[UITabBarController class]]) {
- // 根视图为UITabBarController
-
- currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
-
- } else if ([rootVC isKindOfClass:[UINavigationController class]]){
- // 根视图为UINavigationController
-
- currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
-
- } else {
- // 根视图为非导航类
-
- currentVC = rootVC;
- }
-
- return currentVC;
- }
- + (UIImage *)getImage:(UIView *)shareView
- {
- // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
- if (CGSizeEqualToSize(shareView.frame.size, CGSizeZero)) {
- return nil;
- }
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(shareView.frame.size.width,shareView.frame.size.height ), NO, 0.0);
-
- [shareView.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
-
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
-
- UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
-
- UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
-
- return viewImage;
-
- }
- + (void)clipCorner:(UIRectCorner)corner View:(UIView*)view size:(CGSize)size {
- UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:size]
- ;
- CAShapeLayer *maskLayer = [CAShapeLayer layer];
- maskLayer.frame = view.bounds;
- maskLayer.path = maskPath.CGPath;
- view.layer.mask = maskLayer;
- }
- @end
|