CNLiveMyQrCodeController.m
10.1 KB
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
//
// CNLiveQrCodeController.m
// CNLiveQrCodeModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright (c) 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveMyQrCodeController.h"
#import <Photos/Photos.h>
#import <Accelerate/Accelerate.h>
#import "QMUIKit.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "CNLiveCategory.h"
#import "CNLiveQrCodeNavigationBar.h"
#import "CNLiveMyQrCodeView.h"
#import "CNLiveScanQrCodeController.h"
@interface CNLiveMyQrCodeController ()<CNLiveQrCodeNavigationBarDelegate,QMUIAlertControllerDelegate>
@property (nonatomic, strong) CNLiveQrCodeNavigationBar *navigationBar;
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) CNLiveMyQrCodeView *qrCodeView;
@end
@implementation CNLiveMyQrCodeController
static const NSInteger margin = 20;
static const CGFloat timeValue = 1.5;
#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
if (@available(iOS 11, *)) {
}
else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self createSubViews];
}
- (void)dealloc {
NSLog(@"CNLiveMyQrCodeController -- dealloc");
}
- (void)createSubViews {
[self.view addSubview:self.bgView];
[self.view addSubview:self.qrCodeView];
[self.view addSubview:self.navigationBar];
}
#pragma mark - 响应方法
- (void)goBack {
if (self.presentingViewController != nil) {
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)getPHAuthorizationStatus:(void (^)(void))block{
// 1.获取当前App的相册授权状态
PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
// 2.判断授权状态
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
// 2.1 如果已经授权, 保存图片(调用步骤2的方法)
block();
} else if (authorizationStatus == PHAuthorizationStatusNotDetermined) { // 如果没决定, 弹出指示框, 让用户选择
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
// 如果用户选择授权, 则保存图片
if (status == PHAuthorizationStatusAuthorized) {
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
}
}];
} else {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *app_Name = [infoDict objectForKey:@"CFBundleDisplayName"];
if (app_Name == nil) {
app_Name = [infoDict objectForKey:@"CFBundleName"];
}
NSString *messageString = [NSString stringWithFormat:@"请在iPhone的\"设置 - 隐私 - 相机\"选项中,允许%@访问你的相册", app_Name];
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:messageString preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"好" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:alertA];
[self presentViewController:alertC animated:YES completion:nil];
}
}
// 保存图片
- (void)saveImage:(UIImage *)image{
if(!image){
[QMUITips showError:@"保存失败" inView:self.view hideAfterDelay:timeValue];
return;
}
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
[QMUITips showSucceed:@"保存成功" inView:self.view hideAfterDelay:timeValue];
} else {
[QMUITips showError:@"保存失败" inView:self.view hideAfterDelay:timeValue];
}
});
}];
}
// UIView转UIImage
- (UIImage *)generateImage:(UIView *)view{
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
// 2. 将控制器View的Layer渲染到上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// 3.取出图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 4.结束上下文
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - 代理
/**
* 状态栏的颜色
*
* @return UIStatusBarStyle
*
*/
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (void)navigationBar:(CNLiveQrCodeNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
if ([actionEvents isEqualToString:@"1"]) {
[self goBack];
}
else if ([actionEvents isEqualToString:@"2"]){
self.navigationBar.right.userInteractionEnabled = NO;
__weak typeof(self) weakSelf = self;
QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"保存图片" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[strongSelf getPHAuthorizationStatus:^{
UIImage *image = [strongSelf generateImage:strongSelf.qrCodeView];
[QMUITips showLoadingInView:strongSelf.view];
[strongSelf saveImage:image];
[QMUITips hideAllTipsInView:strongSelf.view];
}];
self.navigationBar.right.userInteractionEnabled = YES;
}];
QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"扫描二维码" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
if(strongSelf.isScanCode){
[strongSelf.navigationController popViewControllerAnimated:YES];
}else{
CNLiveScanQrCodeController *vc = [[CNLiveScanQrCodeController alloc]init];
vc.isMyCode = YES;
[strongSelf.navigationController pushViewController:vc animated:YES];
}
self.navigationBar.right.userInteractionEnabled = YES;
}];
QMUIAlertAction *action3 = [QMUIAlertAction actionWithTitle:@"重置二维码" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[strongSelf.qrCodeView generateQRCode:@"reset"];
self.navigationBar.right.userInteractionEnabled = YES;
}];
QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
self.navigationBar.right.userInteractionEnabled = YES;
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
alertController.delegate = self;
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:action3];
[alertController addAction:cancel];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
action1.buttonAttributes = titleAttributs;
action2.buttonAttributes = titleAttributs;
action3.buttonAttributes = titleAttributs;
cancel.buttonAttributes = cancelAttributs;
[alertController showWithAnimated:YES];
}
}
- (void)didHideAlertController:(QMUIAlertController *)alertController{
self.navigationBar.right.userInteractionEnabled = YES;
}
#pragma mark - 懒加载
- (UIImageView *)bgView{
if(!_bgView){
_bgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
UIImage *image = [UIImage cn_getImageName:@"qr_code_bg" bundleName:@"CNLiveQrCodeModule" targetClass:[CNLiveMyQrCodeController class]];
[_bgView sd_setImageWithURL:[NSURL URLWithString:@"User_Head"] placeholderImage:image completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
}
return _bgView;
}
- (CNLiveMyQrCodeView *)qrCodeView {
if (!_qrCodeView) {
_qrCodeView = [[CNLiveMyQrCodeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-2*margin, SCREEN_WIDTH-2*margin+40)];
_qrCodeView.center = self.view.center;
_qrCodeView.text = @"扫描二维码,加我好友";
}
return _qrCodeView;
}
- (CNLiveQrCodeNavigationBar *)navigationBar {
if (!_navigationBar) {
_navigationBar = [[CNLiveQrCodeNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
_navigationBar.delegate = self;
_navigationBar.title.text = @"我的二维码";
UIImage *image2 = [UIImage cn_getImageName:@"mine_code_more" bundleName:@"CNLiveQrCodeModule" targetClass:[CNLiveMyQrCodeController class]];
[_navigationBar.right setImage:[image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
}
return _navigationBar;
}
@end