// // SPSBSelectSinglePhotoManager.m // 我的社保 // // Created by shanp on 2021/5/13. // #import "SPSBSelectSinglePhotoManager.h" #import #import "SPSBNotificationDelegateManager.h" #import "SPSBBusinessManager.h" #import #import #import "SPSBSelectSinglePhotoViewController.h" #import @interface SPSBSelectSinglePhotoManager () { CGFloat _proportion; __weak UIViewController *_myViewController; bool _isOpenCamera; void (^_completion)(UIImage *_Nullable image, NSValue *_Nullable rect); } @end @implementation SPSBSelectSinglePhotoManager - (instancetype)initWithProportion:(CGFloat)proportion myController:(UIViewController *)myController completion:(void(^)(UIImage *_Nullable image, NSValue *_Nullable rect))completion { self = [super init]; if (!self) return nil; _proportion = proportion; _myViewController = myController; _completion = completion; spsb_setNotificationKey(SPSBAppStatusNotificationKey); @weakify(self) [self addNotificationName:SPSBReopenCameraNotification block:^(id object, NSDictionary *userInfo) { @strongify(self) [self openCamera]; }]; [self addNotificationName:UIApplicationDidEnterBackgroundNotification block:^(id object, NSDictionary *userInfo) { @strongify(self) if (self->_isOpenCamera) { self->_isOpenCamera = false; [self->_myViewController dismissViewControllerAnimated:false completion:nil]; } }]; return self; } - (void)dealloc { spsb_removeNotificationKey(SPSBAppStatusNotificationKey); [self removeAllNotification]; } //打开相机 - (void)openCamera { //判断权限 AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (status == AVAuthorizationStatusNotDetermined) { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { dispatch_async(dispatch_get_main_queue(), ^{ [self openCamera]; }); }]; return; } if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) { @weakify(self) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"需要相机权限" message:@"是否前往设置开启相机权限" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { @strongify(self) [self toSetting]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [_myViewController presentViewController:alert animated:true completion:nil]; return; } //判断相机能否打开 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //打开相机 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; _myViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [_myViewController presentViewController:imagePicker animated:true completion:^{ // NSLog(@"打开相机"); }]; _isOpenCamera = true; } else { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机无法打开" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [_myViewController presentViewController:alert animated:true completion:^{ }]; } } - (void)toSetting { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([jxh_application() canOpenURL:url]) { [jxh_application() openURL:url]; } } //打开相册 - (void)openPhotoAlbum { // if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { spsb_setIsOpenAlbum(true); UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.modalPresentationStyle = UIModalPresentationFullScreen; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [imagePicker setEditing:true animated:true]; //允许编辑 [_myViewController presentViewController:imagePicker animated:true completion:^{ }]; } else { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机无法打开" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [_myViewController presentViewController:alert animated:true completion:^{ }]; } } #pragma mark - 相册调用方法 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { spsb_setIsOpenAlbum(false); _isOpenCamera = false; debugLog(@"cancel"); [picker dismissViewControllerAnimated:true completion:nil]; } //打开相册之后选择的方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //获得媒体类型 NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType]; if ([mediaType hasSuffix:@"image"]) { UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; if (image) { [self pushSelectSinglePhotoViewControllerWithImage:image controller:picker]; return; } NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL]; NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:url.absoluteString]; __block NSString *localIdentifiers; [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.name isEqualToString:@"id"]) { localIdentifiers = obj.value; *stop = true; } }]; if (localIdentifiers) { PHFetchResult * re = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifiers] options:nil]; [[PHCachingImageManager defaultManager] requestImageDataForAsset:re.firstObject options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { UIImage * img = [UIImage imageWithData:imageData]; if (img) { if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { //将照片存入相册中 UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); } [self pushSelectSinglePhotoViewControllerWithImage:img controller:picker]; } }]; } } } - (void)pushSelectSinglePhotoViewControllerWithImage:(UIImage *)image controller:(UIImagePickerController *)picker { if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { //将照片存入相册中 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); } SPSBSelectSinglePhotoViewController *selectSinglePhotoViewController = [[SPSBSelectSinglePhotoViewController alloc] init]; selectSinglePhotoViewController.spsb_needToDismiss = true; selectSinglePhotoViewController.spsb_dataImage = image; selectSinglePhotoViewController.spsb_proportion = _proportion; @weakify(self) [selectSinglePhotoViewController passValue:^(NSValue *value) { @strongify(self) if (value) { self->_isOpenCamera = false; spsb_setIsOpenAlbum(false); self->_completion(image, value); } else { self->_completion(nil, nil); } }]; [picker pushViewController:selectSinglePhotoViewController animated:true]; } #pragma mark - SPSBNotificationDelegate - (void)spsb_shouldDismissAlbum:(void (^)(void))complete { if (spsb_isOpenAlbum()) { spsb_setIsOpenAlbum(false); [_myViewController dismissViewControllerAnimated:false completion:^{ complete(); }]; } } @end