| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- //
- // BGUtils.m
- // BuguLive
- //
- // Created by xfg on 2017/3/14.
- // Copyright © 2017年 xfg. All rights reserved.
- //
- #import "BGUtils.h"
- #import "UIImageView+WebCache.h"
- #import <mach/mach.h>
- #import <Photos/Photos.h>
- @implementation BGUtils
- #pragma mark - ----------------------- 图片 -----------------------
- #pragma mark 下载图片
- + (void)downloadImage:(NSString *)url place:(UIImage *)place imageView:(UIImageView *)imageView
- {
- [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:place options:SDWebImageLowPriority | SDWebImageRetryFailed];
- }
- #pragma marks 画虚线
- + (UIImageView *)dottedLine:(CGRect)frame
- {
- UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
-
-
- UIGraphicsBeginImageContext(imageView1.frame.size); //开始画线
- [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
- CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //设置线条终点形状
-
-
- CGFloat lengths[] = {2,2};
- CGContextRef line = UIGraphicsGetCurrentContext();
- CGContextSetStrokeColorWithColor(line, RGB(200, 200, 200).CGColor);
-
- CGContextSetLineDash(line, 0, lengths, 2); //画虚线
- CGContextMoveToPoint(line, 0.0, frame.size.height); //开始画线
- CGContextAddLineToPoint(line, frame.size.width, frame.size.height);
- CGContextStrokePath(line);
-
- imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
-
- return imageView1;
- }
- #pragma mark 根据颜色生成图片
- + (UIImage*)imageWithColor:(UIColor*)color
- {
- return [BGUtils imageWithColor:color andSize:CGSizeMake(1.0f, 1.0f)];
- }
- #pragma mark 根据颜色返回图片
- + (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
- {
- CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
- #pragma mark 通过图片Data数据第一个字节 来获取图片扩展名
- + (NSString *)contentTypeForImageData:(NSData *)data
- {
- uint8_t c;
- [data getBytes:&c length:1];
- switch (c)
- {
- case 0xFF:
- return @"jpeg";
-
- case 0x89:
- return @"png";
-
- case 0x47:
- return @"gif";
-
- case 0x49:
- case 0x4D:
- return @"tiff";
-
- case 0x52:
- if ([data length] < 12)
- {
- return nil;
- }
-
- NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
- if ([testString hasPrefix:@"RIFF"]
- && [testString hasSuffix:@"WEBP"])
- {
- return @"webp";
- }
- return nil;
- }
- return nil;
- }
- #pragma mark 图片拉伸
- + (UIImage *)resizableImage:(NSString *)imageName
- {
- return [BGUtils resizableImage:imageName edgeInsets:UIEdgeInsetsMake(0.5, 0.5, 0.5, 0.5)];
- }
- #pragma mark 图片拉伸2
- + (UIImage *)resizableImage:(NSString *)imageName edgeInsets:(UIEdgeInsets)edgeInsets
- {
- UIImage *image = [UIImage imageNamed:imageName];
- CGFloat imageW = image.size.width;
- CGFloat imageH = image.size.height;
- return [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageH * edgeInsets.top, imageW * edgeInsets.left, imageH * edgeInsets.bottom, imageW * edgeInsets.right) resizingMode:UIImageResizingModeStretch];
- }
- #pragma mark 获得灰度图
- + (UIImage*)covertToGrayImageFromImage:(UIImage*)sourceImage
- {
- int width = sourceImage.size.width;
- int height = sourceImage.size.height;
-
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
- CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
- CGColorSpaceRelease(colorSpace);
-
- if (context == NULL)
- {
- return nil;
- }
-
- CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
- CGImageRef contextRef = CGBitmapContextCreateImage(context);
- UIImage *grayImage = [UIImage imageWithCGImage:contextRef];
- CGContextRelease(context);
- CGImageRelease(contextRef);
-
- return grayImage;
- }
- #pragma mark 根据bundle中的文件名读取图片
- + (UIImage *)imageWithFileName:(NSString *)name
- {
- NSString *extension = @"png";
-
- NSArray *components = [name componentsSeparatedByString:@"."];
- if ([components count] >= 2)
- {
- NSUInteger lastIndex = components.count - 1;
- extension = [components objectAtIndex:lastIndex];
-
- name = [name substringToIndex:(name.length-(extension.length+1))];
- }
-
- // 如果为Retina屏幕且存在对应图片,则返回Retina图片,否则查找普通图片
- if ([UIScreen mainScreen].scale == 2.0)
- {
- name = [name stringByAppendingString:@"@2x"];
-
- NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
- if (path != nil)
- {
- return [UIImage imageWithContentsOfFile:path];
- }
- }
-
- if ([UIScreen mainScreen].scale == 3.0)
- {
- name = [name stringByAppendingString:@"@3x"];
-
- NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
- if (path != nil)
- {
- return [UIImage imageWithContentsOfFile:path];
- }
- }
-
- NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
- if (path)
- {
- return [UIImage imageWithContentsOfFile:path];
- }
-
- return nil;
- }
- #pragma mark - ----------------------- 字符串 -----------------------
- #pragma mark 是否空字符串
- + (BOOL)isBlankString:(NSString *)string
- {
- if (string == nil || string == NULL)
- {
- return YES;
- }
- if ([string isKindOfClass:[NSNull class]])
- {
- return YES;
- }
- if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
- {
- return YES;
- }
- if ([string isEqualToString:@"(null)"])
- {
- return YES;
- }
- return NO;
- }
- #pragma mark 判断字符串是否由字符串组成
- + (BOOL)isAllNum:(NSString *)string
- {
- unichar c;
- for (int i=0; i<string.length; i++)
- {
- c=[string characterAtIndex:i];
- if (!isdigit(c))
- {
- return NO;
- }
- }
- return YES;
- }
- #pragma mark 判断字符串是否为整数型
- + (BOOL)isPureInt:(NSString *)string
- {
- NSScanner* scan = [NSScanner scannerWithString:string];
- int val;
- return [scan scanInt:&val] && [scan isAtEnd];
- }
- #pragma mark 判断是否为布谷验证字符串
- + (BOOL)isFanwePwd:(NSString*)string
- {
- if([self isPureInt:string])
- {
- if([string length] > 7)
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- else
- {
- return NO;
- }
- }
- #pragma mark 解决字符串乱码问题
- + (NSString*)returnUF8String:(NSString*)str
- {
- if ([str canBeConvertedToEncoding:NSShiftJISStringEncoding])
- {
- str = [NSString stringWithCString:[str cStringUsingEncoding:NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
- }
- return str;
- }
- #pragma mark float保留的位数,并返回string
- + (NSString *)floatReservedString:(float)floatNum markStr:(NSString *)markStr
- {
- int tmpNum = (int)floatNum;
- if (floatNum == tmpNum)
- {
- return [NSString stringWithFormat:@"%@%.f",markStr,floatNum];
- }
- else
- {
- return [NSString stringWithFormat:@"%@%.2f",markStr,floatNum];
- }
- }
- #pragma mark float保留的位数,并返回string
- + (NSString *)floatReservedString:(float)floatNum markBackStr:(NSString *)markBackStr
- {
- int tmpNum = (int)floatNum;
- if (floatNum == tmpNum)
- {
- return [NSString stringWithFormat:@"%.f%@",floatNum,markBackStr];
- }
- else
- {
- return [NSString stringWithFormat:@"%.2f%@",floatNum,markBackStr];
- }
- }
- #pragma mark 判断传入的字符串是否符合HTTP路径的语法规则,即”HTTPS://” 或 “HTTP://”
- + (NSURL *)smartURLForString:(NSString *)str
- {
- NSURL * result;
- NSString * trimmedStr;
- NSRange schemeMarkerRange;
- NSString * scheme;
-
- assert(str != nil);
-
- result = nil;
-
- trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
- schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
-
- if (schemeMarkerRange.location == NSNotFound) {
- result = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
- } else {
- scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
- assert(scheme != nil);
-
- if ( ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame)
- || ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
- result = [NSURL URLWithString:trimmedStr];
- } else {
- // It looks like this is some unsupported URL scheme.
- }
- }
- }
- return result;
- }
- #pragma mark 判断此路径是否能够请求成功
- + (void)validateUrl:(NSURL *)candidate validateResult:(ValidateUrl)validateResult
- {
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:candidate];
- [request setHTTPMethod:@"HEAD"];
- NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
- NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- NSLog(@"error %@",error);
- if (error)
- {
- if (validateResult)
- {
- validateResult(candidate,YES);
- }
- }
- else
- {
- if (validateResult)
- {
- validateResult(candidate,NO);
- }
- }
- }];
- [task resume];
- }
- #pragma mark 移除字符串中的空格和换行
- + (NSString *)removeSpaceAndNewline:(NSString *)str
- {
- NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
- temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
- temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- return temp;
- }
- #pragma mark - ----------------------- 时间 -----------------------
- #pragma mark 返回当前日期字符串
- + (NSString *)dateToString:(NSString*)str
- {
- NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateStyle:NSDateFormatterFullStyle];
- [dateFormatter setDateFormat:str];//
- NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
- return dateString;
- }
- #pragma mark 聊天时间转换
- + (NSString *)formatTime:(NSDate*)compareDate
- {
-
- if( compareDate == nil ) return @"";
-
- NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow];
- timeInterval = -timeInterval;
- long temp = timeInterval;
- NSString *result;
-
- if (timeInterval < 60) {
- if( temp == 0 )
- result = ASLocalizedString(@"刚刚");
- else
- result = [NSString stringWithFormat:ASLocalizedString(@"%d秒前"),(int)temp];
- }
- else if(( timeInterval/60) <60){
- result = [NSString stringWithFormat:ASLocalizedString(@"%d分钟前"),(int)temp/60];
- }
-
- else if(( temp/86400) <30){
-
- NSDateFormatter *date = [[NSDateFormatter alloc] init];
- [date setDateFormat:@"dd"];
- NSString *str = [date stringFromDate:[NSDate date]];
- int nowday = [str intValue];
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"dd"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- int day = [strDate intValue];
- if (nowday-day==0) {
- [dateFormatter setDateFormat:ASLocalizedString(@"今天 HH:mm")];
- result = [dateFormatter stringFromDate:compareDate];
- }
- else if(nowday-day==1)
- {
- [dateFormatter setDateFormat:ASLocalizedString(@"昨天 HH:mm")];
- result = [dateFormatter stringFromDate:compareDate];
- }
- else if( temp < 8 )
- {
- if (temp==1) {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:ASLocalizedString(@"昨天HH:mm")];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
- else if(temp == 2)
- {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:ASLocalizedString(@"前天HH:mm")];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
- }
- else
- {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"MM-dd HH:mm"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
-
- }
- }
- else
- {//超过一个月的就直接显示时间了
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
-
- return result;
- }
- //传入秒得时分秒
- +(NSString *)getMMSSFromSS:(NSString *)totalTime{
-
- NSInteger seconds = [totalTime integerValue];
-
- //format of hour
- NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
- //format of minute
- NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
- //format of second
- NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
- //format of time
- NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
-
- return format_time;
- }
- #pragma mark - ----------------------- 颜色 -----------------------
- #pragma mark 获取随机色
- + (UIColor *)getRandomColor
- {
- return [UIColor colorWithRed:(float)(1+arc4random()%99)/100 green:(float)(1+arc4random()%99)/100 blue:(float)(1+arc4random()%99)/100 alpha:1];
- }
- #pragma mark 十六进制转换为uicolor
- + (UIColor *)colorWithHexString:(NSString *)color
- {
- NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
-
- // String should be 6 or 8 characters
- if ([cString length] < 6)
- {
- return [UIColor clearColor];
- }
-
- // strip 0X if it appears
- if ([cString hasPrefix:@"0X"])
- cString = [cString substringFromIndex:2];
- if ([cString hasPrefix:@"#"])
- cString = [cString substringFromIndex:1];
- if ([cString length] != 6)
- return [UIColor clearColor];
-
- // Separate into r, g, b substrings
- NSRange range;
- range.location = 0;
- range.length = 2;
-
- //r
- NSString *rString = [cString substringWithRange:range];
-
- //g
- range.location = 2;
- NSString *gString = [cString substringWithRange:range];
-
- //b
- range.location = 4;
- NSString *bString = [cString substringWithRange:range];
-
- // Scan values
- unsigned int r, g, b;
- [[NSScanner scannerWithString:rString] scanHexInt:&r];
- [[NSScanner scannerWithString:gString] scanHexInt:&g];
- [[NSScanner scannerWithString:bString] scanHexInt:&b];
-
- return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
- }
- #pragma mark - ----------------------- 转换 -----------------------
- #pragma mark json转NSString
- + (NSString*)dataTOjsonString:(id)object
- {
- NSString *jsonString = nil;
- NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
- options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
- error:&error];
- if (! jsonData)
- {
- NSLog(@"Got an error: %@", error);
- }
- else
- {
- jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- }
- return jsonString;
- }
- #pragma mark 将NSDictionary或NSArray转化为JSON串
- + (NSData *)toJSONData:(id)theData
- {
-
- NSError *error = nil;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
- options:NSJSONWritingPrettyPrinted
- error:&error];
-
- if ([jsonData length] > 0 && error == nil)
- {
- return jsonData;
- }
- else
- {
- return nil;
- }
- }
- #pragma mark 将JSON串转化为NSDictionary
- + (NSDictionary *)jsonStrToDict:(NSString *)jsonStr
- {
- if ([BGUtils isBlankString:jsonStr])
- {
- return nil;
- }
-
- NSError *error;
- NSData *data=[jsonStr dataUsingEncoding:NSUTF8StringEncoding];
- if (data==nil)
- {
- NSLog(ASLocalizedString(@"=======将JSON串转化为NSDictionary失败"));
- }
- else
- {
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
- return dic;
- }
- return nil;
- }
- #pragma mark - ----------------------- 网络 -----------------------
- #pragma mark 判断网络是否连接状态
- + (BOOL)isNetConnected
- {
- BOOL netState;
- switch ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus)
- {
- case AFNetworkReachabilityStatusUnknown:
- netState = NO;
- break;
- case AFNetworkReachabilityStatusNotReachable:
- netState = NO;
- [FanweMessage alert:ASLocalizedString(@"哎呀!网络不大给力!")];
- break;
- case AFNetworkReachabilityStatusReachableViaWWAN:
- {
- netState = YES;
- break;
- }
- case AFNetworkReachabilityStatusReachableViaWiFi:
- netState = YES;
- break;
-
- default:
- break;
- }
- return netState;
- }
- #pragma mark - ----------------------- 软硬件 -----------------------
- #pragma mark 是否打开闪光灯
- + (void)turnOnFlash:(BOOL)isOpen
- {
- AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
-
- if ([device hasTorch])
- {
- if (isOpen)
- {
- [device lockForConfiguration:nil];
- [device setTorchMode: AVCaptureTorchModeOn];
- [device unlockForConfiguration];
- }
- else
- {
- [device lockForConfiguration:nil];
- [device setTorchMode: AVCaptureTorchModeOff];
- [device unlockForConfiguration];
- }
- }
- }
- #pragma mark 获取app的cpu使用情况
- + (float)getAppCpuUsage
- {
- kern_return_t kr;
- task_info_data_t tinfo;
- mach_msg_type_number_t task_info_count;
-
- task_info_count = TASK_INFO_MAX;
- kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
- if (kr != KERN_SUCCESS)
- {
- return -1;
- }
-
- task_basic_info_t basic_info;
- thread_array_t thread_list;
- mach_msg_type_number_t thread_count;
-
- thread_info_data_t thinfo;
- mach_msg_type_number_t thread_info_count;
-
- thread_basic_info_t basic_info_th;
- uint32_t stat_thread = 0; // Mach threads
-
- basic_info = (task_basic_info_t)tinfo;
-
- // get threads in the task
- kr = task_threads(mach_task_self(), &thread_list, &thread_count);
- if (kr != KERN_SUCCESS)
- {
- return -1;
- }
- if (thread_count > 0)
- stat_thread += thread_count;
-
- long tot_sec = 0;
- long tot_usec = 0;
- float tot_cpu = 0;
- int j;
-
- for (j = 0; j < thread_count; j++)
- {
- thread_info_count = THREAD_INFO_MAX;
- kr = thread_info(thread_list[j], THREAD_BASIC_INFO, (thread_info_t)thinfo, &thread_info_count);
- if (kr != KERN_SUCCESS)
- {
- return -1;
- }
-
- basic_info_th = (thread_basic_info_t)thinfo;
-
- if (!(basic_info_th->flags & TH_FLAGS_IDLE))
- {
- tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
- tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
- tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
- }
-
- } // for each thread
-
- kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
- assert(kr == KERN_SUCCESS);
-
- return tot_cpu;
- }
- #pragma mark 统一关闭键盘
- + (void)closeKeyboard
- {
- [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
- // 或者 [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
- }
- #pragma mark 获取app缓存大小
- + (CGFloat)getCachSize
- {
- NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] totalDiskSize];
- //获取自定义缓存大小
- //用枚举器遍历 一个文件夹的内容
- //1.获取 文件夹枚举器
- NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
- NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
- __block NSUInteger count = 0;
- //2.遍历
- for (NSString *fileName in enumerator)
- {
- NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
- NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
- count += fileDict.fileSize;//自定义所有缓存大小
- }
- // 得到是字节 转化为M
- CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
- return totalSize;
- }
- #pragma mark 清理app缓存
- + (void)handleClearView
- {
- //删除两部分
- //1.删除 sd 图片缓存
- //先清除内存中的图片缓存
- [[SDImageCache sharedImageCache] clearMemory];
- //清除磁盘的缓存
- [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
-
- }];
- //2.删除自己缓存
- NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
- [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
- }
- #pragma mark 几个常用的权限判断
- + (void)judgeAuthorization
- {
- if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied)
- {
- NSLog(ASLocalizedString(@"没有定位权限"));
- }
- AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- if (statusVideo == AVAuthorizationStatusDenied)
- {
- NSLog(ASLocalizedString(@"没有摄像头权限"));
- }
- //是否有麦克风权限
- AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
- if (statusAudio == AVAuthorizationStatusDenied)
- {
- NSLog(ASLocalizedString(@"没有录音权限"));
- }
- [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
- if (status == PHAuthorizationStatusDenied)
- {
- NSLog(ASLocalizedString(@"没有相册权限"));
- }
- }];
- }
- #pragma mark - ----------------------- 坐标 -----------------------
- #pragma mark 获取view的坐标在整个window上的位置
- + (CGPoint)getPointInWindow:(UIView *)view viewPoint:(CGPoint)viewPoint
- {
- return [view convertPoint:viewPoint toView:[UIApplication sharedApplication].windows.lastObject];
- // return [v.superview convertPoint:v.frame.origin toView:[UIApplication sharedApplication].windows.lastObject];
- }
- #pragma mark - ----------------------- KVC -----------------------
- #pragma mark kvc 获取所有变量
- + (NSArray *)getAllIvar:(id)object
- {
- NSMutableArray *array = [NSMutableArray array];
-
- unsigned int count;
- Ivar *ivars = class_copyIvarList([object class], &count);
- for (int i = 0; i < count; i++)
- {
- Ivar ivar = ivars[i];
- const char *keyChar = ivar_getName(ivar);
- NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
- @try {
- id valueStr = [object valueForKey:keyStr];
- NSDictionary *dic = nil;
- if (valueStr)
- {
- dic = @{keyStr : valueStr};
- }
- else
- {
- dic = @{keyStr : ASLocalizedString(@"值为nil")};
- }
- [array addObject:dic];
- }
- @catch (NSException *exception) {}
- }
- return [array copy];
- }
- #pragma mark kvc 获得所有属性
- + (NSArray *)getAllProperty:(id)object
- {
- NSMutableArray *array = [NSMutableArray array];
-
- unsigned int count;
- objc_property_t *propertys = class_copyPropertyList([object class], &count);
- for (int i = 0; i < count; i++)
- {
- objc_property_t property = propertys[i];
- const char *nameChar = property_getName(property);
- NSString *nameStr = [NSString stringWithCString:nameChar encoding:NSUTF8StringEncoding];
- [array addObject:nameStr];
- }
- return [array copy];
- }
- +(void)viewShadowPathWithView:(UIView *)view Color:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowPathType:(LeShadowPathType)shadowPathType shadowPathWidth:(CGFloat)shadowPathWidth{
- view.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
- view.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
- view.layer.shadowOpacity = shadowOpacity;// 阴影透明度,默认0
- view.layer.shadowOffset = CGSizeMake(0, -5);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
- // view.layer.shadowRadius = shadowRadius;//阴影半径,默认3
- CGRect shadowRect = CGRectZero;
- CGFloat originX,originY,sizeWith,sizeHeight;
- originX = 0;
- originY = 0;
- sizeWith = view.bounds.size.width;
- sizeHeight = view.bounds.size.height;
- if (shadowPathType == LeShadowPathTop) {
- shadowRect = CGRectMake(originX, originY-shadowPathWidth/2, sizeWith, shadowPathWidth);
- }else if (shadowPathType == LeShadowPathBottom){
- shadowRect = CGRectMake(originY, sizeHeight-shadowPathWidth/2, sizeWith, shadowPathWidth);
- }else if (shadowPathType == LeShadowPathLeft){
- shadowRect = CGRectMake(originX-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
- }else if (shadowPathType == LeShadowPathRight){
- shadowRect = CGRectMake(sizeWith-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
- }else if (shadowPathType == LeShadowPathCommon){
- shadowRect = CGRectMake(originX-shadowPathWidth/2, 2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth/2);
- }else if (shadowPathType == LeShadowPathAround){
- shadowRect = CGRectMake(originX-shadowPathWidth/2, originY-shadowPathWidth/2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth);
- }
- UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
- view.layer.shadowPath = bezierPath.CGPath;//阴影路径
- }
- @end
|