// // TZImagePickerManager.m // YHScrollView_Test // // Created by Jim on 2021/9/3. // #import "TZImagePickerManager.h" #import <TZImagePickerController.h> @interface TZImagePickerManager () <TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{ void(^_cameraImageBlock)(UIImage *selectImage); } @property (nonatomic, strong) TZImagePickerController *imagePickerVC; @property (nonatomic,strong) UIImagePickerController* imagePickerCtrl; @property (nonatomic,strong) UIImage *selectImage; @end @implementation TZImagePickerManager + (instancetype)shareManager { static TZImagePickerManager *manager = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken, ^{ manager = [[TZImagePickerManager alloc] init]; }); return manager; } -(void)pushPickerViewController:(PickerViewControllerType)type fromViewController:(UIViewController *)vc selectImageNumber:(NSInteger) imageNumber complete:(nonnull void (^)(NSArray<UIImage *> * _Nonnull, NSArray * _Nonnull, BOOL))completeBlock{ switch (type) { case PickerViewControllerTypeOfImageCrop:{ dispatch_async(dispatch_get_main_queue(), ^{ //用户设置头像 self.imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self pushPhotoPickerVc:YES]; self.imagePickerVC.allowTakePicture = NO; // 在内部显示拍照按钮 self.imagePickerVC.allowTakeVideo = NO; // 在内部显示拍视频按 self.imagePickerVC.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0]; self.imagePickerVC.showPhotoCannotSelectLayer = YES; self.imagePickerVC.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8]; self.imagePickerVC.allowPickingVideo = NO; self.imagePickerVC.allowPickingImage = YES; self.imagePickerVC.allowPickingOriginalPhoto = NO; self.imagePickerVC.allowPickingGif = NO; self.imagePickerVC.allowPickingMultipleVideo = NO; // 是否可以多选视频 self.imagePickerVC.sortAscendingByModificationDate = NO; self.imagePickerVC.showSelectBtn = NO; self.imagePickerVC.allowCrop = YES; self.imagePickerVC.needCircleCrop = YES; // 设置竖屏下的裁剪尺寸 NSInteger left = 30; NSInteger widthHeight = KScreenWidth - 2 * left; NSInteger top = (KScreenHeight - widthHeight) / 2; self.imagePickerVC.cropRect = CGRectMake(left, top, widthHeight, widthHeight); self.imagePickerVC.scaleAspectFillCrop = YES; self.imagePickerVC.statusBarStyle = UIStatusBarStyleLightContent; self.imagePickerVC.showSelectedIndex = NO; [self.imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { if (completeBlock) { completeBlock(photos, assets, isSelectOriginalPhoto); } }]; self.imagePickerVC.modalPresentationStyle = UIModalPresentationFullScreen; [vc presentViewController:self.imagePickerVC animated:YES completion:nil]; }); } break; case PickerViewControllerTypeOfImageDefault:{ dispatch_async(dispatch_get_main_queue(), ^{ self.imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:imageNumber delegate:self]; self.imagePickerVC.allowTakePicture = NO; // 在内部显示拍照按钮 self.imagePickerVC.allowTakeVideo = NO; // 在内部显示拍视频按 self.imagePickerVC.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0]; self.imagePickerVC.showPhotoCannotSelectLayer = YES; self.imagePickerVC.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8]; self.imagePickerVC.allowPickingVideo = NO; self.imagePickerVC.allowPickingImage = YES; self.imagePickerVC.allowPickingOriginalPhoto = NO; self.imagePickerVC.allowPickingGif = NO; self.imagePickerVC.allowPickingMultipleVideo = NO; // 是否可以多选视频 self.imagePickerVC.sortAscendingByModificationDate = NO; self.imagePickerVC.showSelectBtn = NO; self.imagePickerVC.allowCrop = NO; self.imagePickerVC.needCircleCrop = NO; // 设置竖屏下的裁剪尺寸 NSInteger left = 30; NSInteger widthHeight = KScreenWidth - 2 * left; NSInteger top = (KScreenHeight - widthHeight) / 2; self.imagePickerVC.cropRect = CGRectMake(left, top, widthHeight, widthHeight); self.imagePickerVC.scaleAspectFillCrop = YES; self.imagePickerVC.statusBarStyle = UIStatusBarStyleLightContent; self.imagePickerVC.showSelectedIndex = YES; [self.imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { if (completeBlock) { completeBlock(photos, assets, isSelectOriginalPhoto); } }]; self.imagePickerVC.modalPresentationStyle = UIModalPresentationFullScreen; [vc presentViewController:self.imagePickerVC animated:YES completion:nil]; }); } break; default: break; } } -(void)pushCameraViewController:(UIViewController *)currentVC complete:(void (^)(UIImage * _Nonnull))cameraFinshBlock{ _cameraImageBlock = cameraFinshBlock; self.imagePickerCtrl.modalPresentationStyle = UIModalPresentationFullScreen; [Untis judgeIsHaveCamerCompleteBlock:^(BOOL granted) { if (granted) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { dispatch_async(dispatch_get_main_queue(), ^{ self.imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera; }); dispatch_async(dispatch_get_main_queue(), ^{ self.imagePickerCtrl.modalPresentationStyle = UIModalPresentationFullScreen; [currentVC presentViewController:self.imagePickerCtrl animated:YES completion:NULL]; }); } }else{ dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"APP想访问您的相机" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [actionSheet addAction:action1]; [actionSheet addAction:cancleAction]; [currentVC presentViewController:actionSheet animated:YES completion:nil]; }); } }]; } #pragma mark -UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *image = info[UIImagePickerControllerOriginalImage]; if(!image){ return; } __weak typeof(self) weakSelf = self; if (picker.sourceType==UIImagePickerControllerSourceTypeCamera){ __strong typeof(weakSelf) strongSelf = weakSelf; //相机拍摄 [self.imagePickerCtrl dismissViewControllerAnimated:YES completion:^{ strongSelf.imagePickerCtrl=nil; strongSelf.selectImage = image; strongSelf->_cameraImageBlock(image); }]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { if ([UIDevice currentDevice].systemVersion.doubleValue >= 13.0) {//避免ios 12.2返回有动画时大厅头部信息布局错乱 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDarkContent]; }else{ [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; } [picker dismissViewControllerAnimated:YES completion:^{ self.imagePickerCtrl = nil; }]; } -(UIImagePickerController*)imagePickerCtrl { if (!_imagePickerCtrl) { _imagePickerCtrl = [[UIImagePickerController alloc] init]; [_imagePickerCtrl.navigationBar setBarTintColor:UIColorFromRGB(0xffffff)]; [_imagePickerCtrl.navigationBar setTintColor:UIColorFromRGB(0x000000)]; NSDictionary * dict = @{NSForegroundColorAttributeName:UIColorFromRGB(0x000000), NSFontAttributeName:[UIFont systemFontOfSize:15] }; _imagePickerCtrl.modalPresentationStyle = UIModalPresentationFullScreen; _imagePickerCtrl.navigationBar.titleTextAttributes = dict; _imagePickerCtrl.delegate=self; } return _imagePickerCtrl; } @end