萤石云 iOSSDK,移植跨平台相关工程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
//  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