// // LYLPhotoTailoringTool.m // PhotoTailoring // // Created by Rainy on 2018/8/22. // Copyright © 2018年 Rainy. All rights reserved. // #import "LYLPhotoTailoringTool.h" #import "LYLPhotoTailoringViewController.h" #import #import @interface LYLPhotoTailoringTool () { ChoosImageBlock _choosImageBlock; ShowImageBlock _showImageBlock; UIImagePickerControllerSourceType _photoType; BOOL _crop; } @end @implementation LYLPhotoTailoringTool static LYLPhotoTailoringTool *tool = nil; + (instancetype)sharedTool { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ tool = [LYLPhotoTailoringTool new]; }); return tool; } //- (void)photoTailoring:(ChoosImageBlock)finished - (void)photoTailoringAndShowImg:(NSString *)imgUrl showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock { _crop=YES; _choosImageBlock = choosImgBlock; _showImageBlock = showImgBlock; UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; if(imgUrl){ [alertController addAction:[UIAlertAction actionWithTitle:@"查看大图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { _showImageBlock(imgUrl); }]]; } [alertController addAction:[UIAlertAction actionWithTitle:@"拍一张" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self choosePhoto:NO]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self choosePhoto:YES]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]]; [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil]; } - (void)photoTailoringAndShowImg:(NSString *)imgUrl crop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock { _crop=crop; _choosImageBlock = choosImgBlock; _showImageBlock = showImgBlock; UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; if(imgUrl){ [alertController addAction:[UIAlertAction actionWithTitle:@"查看大图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { _showImageBlock(imgUrl); }]]; } [alertController addAction:[UIAlertAction actionWithTitle:@"拍一张" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self choosePhoto:NO]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self choosePhoto:YES]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]]; [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil]; } - (void)selectPhotoWithPhoroOrCamera:(SelectPhotoWithTypePhoroOrCamera)type crop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock { _crop=crop; _choosImageBlock = choosImgBlock; _showImageBlock = showImgBlock; if(type == SelectPhotoWithTypePhoroOrCamera1){ [self choosePhoto:YES]; }else if (type == SelectPhotoWithTypePhoroOrCamera2){ [self choosePhoto:NO]; } } //直接打开相册 - (void)openPhoroWithCrop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock{ _crop=crop; _choosImageBlock = choosImgBlock; _showImageBlock = showImgBlock; [self choosePhoto:YES]; } //直接打开相机 - (void)openCameraWithCrop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock{ _crop=crop; _choosImageBlock = choosImgBlock; _showImageBlock = showImgBlock; [self choosePhoto:NO]; } - (void)chooseImage:(ChoosImageBlock)choosImgBlock { _choosImageBlock = choosImgBlock; [self choosePhoto:YES]; } - (void)choosePhoto:(BOOL)photo { WS(weakSelf) __block UIImagePickerControllerSourceType block_photoType = _photoType; [self checkCheckPermissionsType:(photo ? CheckPermissionsTypePhoto : CheckPermissionsTypeVideo) permissionGranted:^{ UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.sourceType = photo ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera; block_photoType = ipc.sourceType; ipc.delegate = weakSelf; ipc.allowsEditing = NO; ipc.modalTransitionStyle = [self getCurrentViewController].navigationController.modalTransitionStyle; ipc.modalPresentationStyle = [UtilsTools modalPresentationStyle:[self getCurrentViewController].navigationController.modalPresentationStyle]; // [ipc.navigationBar setBarStyle:UIBarStyleBlack]; // UIViewController * vc = [[UIViewController alloc] init]; // [vc.view addSubview:ipc.view]; // UIButton * btn = [UIButton new]; // [btn setTitle:@"取消" forState:UIControlStateNormal]; // btn.titleLabel.font = [UIFont systemFontOfSize:14]; // [vc.view addSubview:btn]; // [btn mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.mas_offset(IS_IPHONEX ? 44 : 20); // make.size.mas_offset(CGSizeMake(60, 44)); // make.right.mas_offset(-10); // }]; // [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // [btn setAction:^{ // [vc dismissViewControllerAnimated:YES completion:^{ // // }]; // }]; [[self getCurrentViewController] presentViewController:ipc animated:YES completion:nil]; } noPermission:^(CheckPermissionsType type) { NSString *message = @""; if (type == CheckPermissionsTypePhoto) { message = @"请在设置中,允许“灵犀办公”访问您的照片"; } if (type == CheckPermissionsTypeVideo) { message = @"请在设置中,允许“灵犀办公”访问您的相机"; } UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication]canOpenURL:url]) { [[UIApplication sharedApplication]openURL:url]; } }]]; [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil]; }]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; if (_crop==NO) { _choosImageBlock(image); [picker dismissViewControllerAnimated:YES completion:nil]; return; } LYLPhotoTailoringViewController *photoVC = [[LYLPhotoTailoringViewController alloc] init]; photoVC.oldImage = image; photoVC.mode = PhotoMaskViewModeSquare; photoVC.cropWidth = CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds); photoVC.cropHeight = CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds); photoVC.delegate = self; photoVC.lineColor = [UIColor clearColor]; [picker pushViewController:photoVC animated:YES]; // if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { // UIImageWriteToSavedPhotosAlbum(image, self, nil, NULL); // } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - photoViewControllerDelegate - (void)imageCropperDidCancel:(LYLPhotoTailoringViewController *)cropperViewController { if (_photoType == UIImagePickerControllerSourceTypePhotoLibrary) { [cropperViewController dismissViewControllerAnimated:YES completion:nil]; }else{ [cropperViewController dismissViewControllerAnimated:YES completion:nil]; } } - (void)imageCropper:(LYLPhotoTailoringViewController *)cropperViewController didFinished:(UIImage *)editedImage { __weak ChoosImageBlock block_choosImageBlock = _choosImageBlock; [cropperViewController dismissViewControllerAnimated:YES completion:^{ block_choosImageBlock(editedImage); }]; } - (UIViewController *)getCurrentViewController { __block UIWindow *normalWindow = [[UIApplication sharedApplication].delegate window]; if (normalWindow.windowLevel != UIWindowLevelNormal) { [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.windowLevel == UIWindowLevelNormal) { normalWindow = obj; *stop = YES; } }]; } return [self nextTopForViewController:normalWindow.rootViewController]; } - (UIViewController *)nextTopForViewController:(UIViewController *)inViewController { while (inViewController.presentedViewController) { inViewController = inViewController.presentedViewController; } if ([inViewController isKindOfClass:[UITabBarController class]]) { UIViewController *selectedVC = [self nextTopForViewController:((UITabBarController *)inViewController).selectedViewController]; return selectedVC; } else if ([inViewController isKindOfClass:[UINavigationController class]]) { UIViewController *selectedVC = [self nextTopForViewController:((UINavigationController *)inViewController).visibleViewController]; return selectedVC; } else { return inViewController; } } typedef NS_ENUM(NSUInteger, CheckPermissionsType) { CheckPermissionsTypePhoto, CheckPermissionsTypeVideo, }; - (void)checkCheckPermissionsType:(CheckPermissionsType)type permissionGranted:(void (^)(void))permissionGranted noPermission:(void (^)(CheckPermissionsType type))noPermission { if (type == CheckPermissionsTypePhoto) { PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus]; if (photoAuthStatus == PHAuthorizationStatusNotDetermined) { [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission(type); }); }]; }else if (photoAuthStatus == PHAuthorizationStatusRestricted || photoAuthStatus == PHAuthorizationStatusDenied){ noPermission(type); }else { permissionGranted(); } } if (type == CheckPermissionsTypeVideo) { AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (videoAuthStatus == AVAuthorizationStatusNotDetermined) { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { dispatch_async(dispatch_get_main_queue(), ^{ granted ? permissionGranted() : noPermission(type); }); }]; }else if (videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied){ noPermission(type); }else { permissionGranted(); } } } @end