PhotoAlertView.m 5.5 KB
//
//  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