//
|
// Constants.m
|
// SDKDemo
|
//
|
// Created by Tim Lei on 10/13/15.
|
// Copyright © 2015 FreeView. All rights reserved.
|
//
|
|
#import "HDLEZConstants.h"
|
#import <Photos/Photos.h>
|
@implementation HDLEZConstants
|
/*获取当前controller
|
*/
|
+(UIViewController *)currentVC{
|
UIViewController *currentVC;
|
UIViewController *rootVC=[self appWindow].rootViewController;
|
if ([rootVC presentedViewController]) {
|
rootVC=[rootVC presentedViewController];
|
}
|
if ([rootVC isKindOfClass:[UITabBarController class]]) {
|
currentVC=[(UITabBarController*)rootVC selectedViewController];
|
}else if([rootVC isKindOfClass:[UINavigationController class]]){
|
currentVC=[(UINavigationController*)rootVC visibleViewController];
|
}else{
|
currentVC=rootVC;
|
}
|
return currentVC;
|
}
|
|
/*获取当前window
|
*/
|
+(UIWindow*)appWindow{
|
UIWindow *window;
|
if (@available(iOS 13.0, *)) {
|
HDLEZLog(@"windows:%@",[UIApplication sharedApplication].windows);
|
window = [UIApplication sharedApplication].windows[0];
|
if (!window) {
|
window=[UIApplication sharedApplication].delegate.window;
|
}
|
} else {
|
window = [UIApplication sharedApplication].delegate.window;
|
}
|
return window;
|
}
|
|
|
+(UIImage *)captureImageFromView:(UIView *)view{
|
|
// // currentView 当前的view 创建一个基于位图的图形上下文并指定大小为
|
// UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height), NO, 0.0);
|
// // renderInContext呈现接受者及其子范围到指定的上下文
|
// [view.layer renderInContext:UIGraphicsGetCurrentContext()];
|
// // 返回一个基于当前图形上下文的图片
|
// UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
|
// // 移除栈顶的基于当前位图的图形上下文
|
// UIGraphicsEndImageContext();
|
// // 保存图片
|
// //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
|
// return viewImage;
|
|
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
|
|
// Render our snapshot into the image context
|
|
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
|
|
// Grab the image from the context
|
|
UIImage *complexViewImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
// Finish using the context
|
|
UIGraphicsEndImageContext();
|
|
return complexViewImage;
|
|
|
}
|
|
#pragma 保存图片到相册
|
+(void)saveImageToPhotosAlbum:(UIImage *)savedImage
|
{
|
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
|
if (status == PHAuthorizationStatusNotDetermined)
|
{
|
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
|
if(status == PHAuthorizationStatusAuthorized)
|
{
|
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL);
|
}
|
}];
|
}
|
else
|
{
|
if (status == PHAuthorizationStatusAuthorized)
|
{
|
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL);
|
}
|
}
|
}
|
|
// 保存到相册指定回调方法
|
+(void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
|
{
|
NSString *message = nil;
|
NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
|
NSString *saveToTheAlbumsStr,*operationFailedStr,*tipStr,*okStr;
|
// 简体中文
|
if ([languageName rangeOfString:@"zh-Hans"].location != NSNotFound) {
|
tipStr = @"提示";
|
saveToTheAlbumsStr = @"已保存至手机相册.";
|
operationFailedStr = @"操作失败";
|
okStr = @"确认";
|
|
}else{
|
tipStr = @"Prompt";
|
okStr = @"OK";
|
saveToTheAlbumsStr = @"Saved to the albums.";
|
operationFailedStr = @"Operation failed.";
|
|
}
|
if (!error) {
|
message = saveToTheAlbumsStr;
|
}
|
else
|
{
|
message = operationFailedStr;
|
}
|
|
[self showUIAlertView:message title:tipStr cancelTitle:okStr];
|
}
|
|
+(void)showUIAlertView:(NSString *)mes title:(NSString *)title cancelTitle:(NSString *)cancleTitle
|
{
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:mes preferredStyle:UIAlertControllerStyleAlert];
|
[alertController addAction:[UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleCancel handler:nil]];
|
[[self topMostController] presentViewController:alertController animated:YES completion:nil];
|
|
}
|
|
+(UIViewController *) topMostController {
|
UIViewController*topController ;
|
if ([UIApplication sharedApplication].delegate.window) {
|
topController= [UIApplication sharedApplication].delegate.window.rootViewController;
|
}else{
|
topController=[self appWindow].rootViewController;
|
}
|
while(topController.presentedViewController){
|
topController=topController.presentedViewController;
|
}
|
return topController;
|
}
|
|
/**
|
* 生成二维码
|
*/
|
+ (UIImage *)creatCIQRCodeImage:(NSString *)dataStr{
|
//创建过滤器,这里的@"CIQRCodeGenerator"是固定的
|
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
|
//恢复默认设置
|
[filter setDefaults];
|
//给过滤器添加数据
|
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
|
//value必须是NSData类型
|
[filter setValue:data forKeyPath:@"inputMessage"];
|
//生成二维码
|
CIImage *outputImage = [filter outputImage];
|
//显示二维码
|
return [self creatNonInterpolatedUIImageFormCIImage:outputImage withSize:100.0];
|
}
|
|
+ (UIImage *)creatNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size {
|
CGRect extent = CGRectIntegral(image.extent);
|
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
|
//创建bitmap
|
size_t width = CGRectGetWidth(extent) * scale;
|
size_t height = CGRectGetHeight(extent) * scale;
|
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
|
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
|
CIContext *context = [CIContext contextWithOptions:nil];
|
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
|
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
|
CGContextScaleCTM(bitmapRef, scale, scale);
|
CGContextDrawImage(bitmapRef, extent, bitmapImage);
|
//保存bitmap图片
|
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
|
CGContextRelease(bitmapRef);
|
CGImageRelease(bitmapImage);
|
return [UIImage imageWithCGImage:scaledImage];
|
}
|
|
/*获取特定时间格式的时间字符串
|
*/
|
+(NSString *)timeStrWithFormate:(NSString *)formate data:(NSDate *)date{
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateFormat:formate];
|
NSString *dateString = [dateFormatter stringFromDate:date];
|
return dateString;
|
}
|
|
/**
|
计算文字高度,允许换行计算
|
|
@param fontSize 文字大小
|
@param widht 文字宽度
|
@param text 文字内容
|
@return 返回文字的高度
|
*/
|
+(CGFloat)sizeLineFeedWithFont:(CGFloat)fontSize textSizeWidht:(CGFloat)widht text:(NSString*)text{
|
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, widht, 0)];
|
textView.text = text;
|
textView.font = [UIFont systemFontOfSize:fontSize];
|
CGSize size = CGSizeMake(widht, MAXFLOAT);
|
CGSize constraint = [textView sizeThatFits:size];
|
return constraint.height;
|
}
|
|
/**
|
* 保存数据
|
*/
|
+ (void)saveObject:(id )obj key:(NSString*)key{
|
NSUserDefaults *UD=[NSUserDefaults standardUserDefaults];
|
[UD setObject:obj forKey:key];
|
[UD synchronize];
|
}
|
|
/**
|
* 获取数据
|
*/
|
+ (id)getObjectBykey:(NSString*)key{
|
NSUserDefaults *UD=[NSUserDefaults standardUserDefaults];
|
return [UD objectForKey:key];
|
}
|
|
/*小于两位数前面自动填补0
|
*/
|
+(NSString *)autoFillZero:(NSInteger)value;{
|
NSString *result=@"";
|
if (value<10) {
|
result=[NSString stringWithFormat:@"0%ld",(long)value];
|
}else{
|
result=[NSString stringWithFormat:@"%ld",(long)value];
|
}
|
return result;
|
}
|
|
/*时间对比
|
*/
|
+ (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
|
{
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
|
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
|
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
|
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
|
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
|
NSComparisonResult result = [dateA compare:dateB];
|
NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
|
if (result == NSOrderedDescending) {
|
//在指定时间前面 过了指定时间 过期
|
return 1;
|
}
|
else if (result == NSOrderedAscending){
|
//没过指定时间 没过期
|
//NSLog(@"Date1 is in the past");
|
return -1;
|
}
|
//刚好时间一样.
|
//NSLog(@"Both dates are the same");
|
return 0;
|
|
}
|
|
/*时间格式转换
|
*/
|
+(NSString *)turnDateString:(NSString*)dateString toFormater:(NSString*)formater{
|
//原始时间
|
NSDateFormatter *originDateFormatter = [[NSDateFormatter alloc] init];
|
[originDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
|
NSDate *oringinDate = [originDateFormatter dateFromString:dateString];
|
|
//转换目标时间
|
NSDateFormatter *targetDateFormatter = [[NSDateFormatter alloc] init];
|
[targetDateFormatter setDateFormat:formater];
|
NSString *targetDateStr = [targetDateFormatter stringFromDate:oringinDate];
|
|
|
return targetDateStr;
|
}
|
|
|
/*判断是否为纯数字
|
@param number 内容
|
*/
|
+(BOOL)validateNumber:(NSString*)number{
|
BOOL res = YES;
|
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
|
int i = 0;
|
while (i < number.length) {
|
NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
|
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
|
if (range.length == 0) {
|
res = NO;
|
break;
|
}
|
i++;
|
}
|
return res;
|
}
|
|
/* 将nil转为""
|
*/
|
+(NSString*)turnNil:(NSString *)str{
|
if (str == nil || str == NULL) {
|
return @"";
|
}else{
|
return str;
|
}
|
}
|
@end
|