// // 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