chenqiyang
2021-08-27 6a63c4281fbe7e17103115320cd276397d733081
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
//
//  HDLLinPhoneUtils.m
//  HDLLinPhoneSDK
//
//  Created by 陈启扬 on 2021/8/5.
//  Copyright © 2021 陈启扬. All rights reserved.
//
 
#import "HDLLinPhoneCommon.h"
#import <Photos/Photos.h>
@implementation HDLLinPhoneCommon
+(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;
}
 
+(UIWindow*)appWindow{
    UIWindow *window;
    if (@available(iOS 13.0, *)) {
        window = [UIApplication sharedApplication].windows[0];
        if (!window) {
            window=[UIApplication sharedApplication].delegate.window;
//            DT(@"13delegateWindow:%@",window);
        }
//        DT(@"window:%@",window);
    } else {
        window = [UIApplication sharedApplication].delegate.window;
//        DT(@"delegateWindow:%@",window);
    }
    return window;
}
 
+(NSString *)temporarySaveImagePath{
    NSString *str;
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSLog(@"path:%@",path);
    NSString *pictureName = @"snapShot.jpg";
    str=[NSString stringWithFormat:@"%@/%@",path[0],pictureName];
    return str;
}
 
#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];
    
}
 
//+(UIImage *)captureImageFromView:(UIView *)view
//{
//    CGRect screenRect = [view bounds];
//    UIGraphicsBeginImageContext(screenRect.size);
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    [view.layer renderInContext:ctx];
//    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return image;
//}
 
+(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;
 
 
}
@end