PhotoAlertView.m
5.5 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
//
// PhotoAlertView.m
// DreamSleep
//
// Created by peter on 2022/4/20.
//
#import "PhotoAlertView.h"
#import "JWMImagePickerController.h"
#import "RSKImageCropper.h"
#import <AVFoundation/AVFoundation.h>
@interface PhotoAlertView () <JWMImagePickerDelegate, RSKImageCropViewControllerDelegate>
@property (nonatomic, strong) UIView *photoView;
@property (nonatomic, strong) JWMImagePickerController *imgePicker;
@end
@implementation PhotoAlertView {
UIViewController *_tmpVc;
}
- (instancetype)initWithVC:(UIViewController *)vc {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
_tmpVc = vc;
self.backgroundColor = AlertDarkColor;
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.6];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self addGestureRecognizer:tapGR];
[self addSubview:self.photoView];
[self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.left.equalTo(self).offset(15);
make.right.equalTo(self).offset(-15);
make.bottom.equalTo(self).offset(- 118 - Bottom_SafeArea_Height);
make.height.equalTo(@106);
}];
UIButton *photoBtn = [self btnWithTitle:@"拍照" index:1 superView:self.photoView];
UIButton *albumBtn = [self btnWithTitle:@"从相册中选择" index:2 superView:self.photoView];
UIButton *dismissBtn = [self btnWithTitle:@"取消" index:3 superView:self];
[dismissBtn cornerRadius:12.0];
[photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.photoView);
make.centerX.equalTo(self.photoView);
make.left.right.equalTo(self.photoView);
}];
[albumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.photoView);
make.left.right.equalTo(self.photoView);
make.size.equalTo(photoBtn);
make.top.equalTo(photoBtn.mas_bottom);
make.bottom.equalTo(self.photoView);
}];
[dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.photoView);
make.left.right.equalTo(photoBtn);
make.size.equalTo(photoBtn);
make.top.equalTo(self.photoView.mas_bottom).offset(20);
}];
}
return self;
}
- (UIButton *)btnWithTitle:(NSString *)title index:(int)index superView:(UIView *)sv {
UIButton *btn = [UIButton dkBtnTitle:title font:BoldFont(15)];
btn.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
btn.tag = index;
[btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];
return btn;
}
- (void)clickAction:(UIButton *)sender {
[self dismissPhotoAlertView];
if (sender.tag == 1) { // 拍照
// 相机鉴权
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
[DSProgressHUD showDetailInfo:@"请前往iPhone的“设置-隐私-相机”,允许APP访问您的相机。" ];
return;
}
} else {
[DSProgressHUD showDetailInfo:@"您的设备无摄像头!" ];
return;
}
[self presentViewController:ImagePickerStyleCamera];
} else if (sender.tag == 2) { // 从相册中选取
[self presentViewController:ImagePickerStylePhotoLibrary];
}
}
- (void)presentViewController:(ImagePickerStyle)style {
self.imgePicker = [[JWMImagePickerController alloc] initWithImagePickerStyle:style delegate:self];
self.imgePicker.navigationBar.barTintColor = style == ImagePickerStyleCamera ? DSBlack : DSWhite;
[_tmpVc presentViewController:self.imgePicker animated:YES completion:nil];
}
- (void)tapAction:(UITapGestureRecognizer *)gesture {
if (!CGRectContainsPoint(self.photoView.frame, [gesture locationInView:self])) {
[self dismissPhotoAlertView];
}
}
#pragma mark - JWMImagePickerDelegate
- (void)jwmImagePickerImageFinished:(UIImage *)image {
// 跳转到头像剪切页面
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image cropMode:RSKImageCropModeCircle];
imageCropVC.delegate = self;
[_tmpVc presentViewController:imageCropVC animated:YES completion:nil];
}
#pragma mark - RSKImageCropViewControllerDelegate
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller {
[_tmpVc dismissViewControllerAnimated:YES completion:nil];
}
- (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect {
/** 用户头像上传,成功后存入沙盒 */
[_tmpVc dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - public
- (void)showPhotoAlertView {
[DSKeyWindow addSubview:self];
}
- (void)dismissPhotoAlertView {
[self removeFromSuperview];
}
#pragma mark - lazy
- (UIView *)photoView {
if (!_photoView) {
_photoView = [UIView new];
[_photoView cornerRadius:12.0];
_photoView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
}
return _photoView;
}
@end