SPSBSelectSinglePhotoManager.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // SPSBSelectSinglePhotoManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/13.
  6. //
  7. #import "SPSBSelectSinglePhotoManager.h"
  8. #import <NSObject+JXHNotification.h>
  9. #import "SPSBNotificationDelegateManager.h"
  10. #import "SPSBBusinessManager.h"
  11. #import <Photos/Photos.h>
  12. #import <JXHSystemShortcut.h>
  13. #import "SPSBSelectSinglePhotoViewController.h"
  14. #import <JXHMacro.h>
  15. @interface SPSBSelectSinglePhotoManager ()<SPSBNotificationDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
  16. CGFloat _proportion;
  17. __weak UIViewController *_myViewController;
  18. bool _isOpenCamera;
  19. void (^_completion)(UIImage *_Nullable image, NSValue *_Nullable rect);
  20. }
  21. @end
  22. @implementation SPSBSelectSinglePhotoManager
  23. - (instancetype)initWithProportion:(CGFloat)proportion myController:(UIViewController *)myController completion:(void(^)(UIImage *_Nullable image, NSValue *_Nullable rect))completion {
  24. self = [super init];
  25. if (!self) return nil;
  26. _proportion = proportion;
  27. _myViewController = myController;
  28. _completion = completion;
  29. spsb_setNotificationKey(SPSBAppStatusNotificationKey);
  30. @weakify(self)
  31. [self addNotificationName:SPSBReopenCameraNotification block:^(id object, NSDictionary *userInfo) {
  32. @strongify(self)
  33. [self openCamera];
  34. }];
  35. [self addNotificationName:UIApplicationDidEnterBackgroundNotification block:^(id object, NSDictionary *userInfo) {
  36. @strongify(self)
  37. if (self->_isOpenCamera) {
  38. self->_isOpenCamera = false;
  39. [self->_myViewController dismissViewControllerAnimated:false completion:nil];
  40. }
  41. }];
  42. return self;
  43. }
  44. - (void)dealloc {
  45. spsb_removeNotificationKey(SPSBAppStatusNotificationKey);
  46. [self removeAllNotification];
  47. }
  48. //打开相机
  49. - (void)openCamera {
  50. //判断权限
  51. AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  52. if (status == AVAuthorizationStatusNotDetermined) {
  53. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. [self openCamera];
  56. });
  57. }];
  58. return;
  59. }
  60. if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
  61. @weakify(self)
  62. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"需要相机权限" message:@"是否前往设置开启相机权限" preferredStyle:UIAlertControllerStyleAlert];
  63. [alert addAction:[UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  64. @strongify(self)
  65. [self toSetting];
  66. }]];
  67. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  68. }]];
  69. [_myViewController presentViewController:alert animated:true completion:nil];
  70. return;
  71. }
  72. //判断相机能否打开
  73. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  74. //打开相机
  75. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  76. imagePicker.delegate = self;
  77. imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  78. _myViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  79. [_myViewController presentViewController:imagePicker animated:true completion:^{
  80. // NSLog(@"打开相机");
  81. }];
  82. _isOpenCamera = true;
  83. } else {
  84. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机无法打开" preferredStyle:UIAlertControllerStyleAlert];
  85. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  86. }]];
  87. [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  88. }]];
  89. [_myViewController presentViewController:alert animated:true completion:^{
  90. }];
  91. }
  92. }
  93. - (void)toSetting {
  94. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  95. if ([jxh_application() canOpenURL:url]) {
  96. [jxh_application() openURL:url];
  97. }
  98. }
  99. //打开相册
  100. - (void)openPhotoAlbum {
  101. //
  102. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
  103. spsb_setIsOpenAlbum(true);
  104. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  105. imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
  106. imagePicker.delegate = self;
  107. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  108. [imagePicker setEditing:true animated:true]; //允许编辑
  109. [_myViewController presentViewController:imagePicker animated:true completion:^{
  110. }];
  111. } else {
  112. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"相机无法打开" preferredStyle:UIAlertControllerStyleAlert];
  113. [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  114. }]];
  115. [_myViewController presentViewController:alert animated:true completion:^{
  116. }];
  117. }
  118. }
  119. #pragma mark - 相册调用方法
  120. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  121. spsb_setIsOpenAlbum(false);
  122. _isOpenCamera = false;
  123. debugLog(@"cancel");
  124. [picker dismissViewControllerAnimated:true completion:nil];
  125. }
  126. //打开相册之后选择的方法
  127. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  128. //获得媒体类型
  129. NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
  130. if ([mediaType hasSuffix:@"image"]) {
  131. UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
  132. if (image) {
  133. [self pushSelectSinglePhotoViewControllerWithImage:image controller:picker];
  134. return;
  135. }
  136. NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL];
  137. NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:url.absoluteString];
  138. __block NSString *localIdentifiers;
  139. [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  140. if ([obj.name isEqualToString:@"id"]) {
  141. localIdentifiers = obj.value;
  142. *stop = true;
  143. }
  144. }];
  145. if (localIdentifiers) {
  146. PHFetchResult * re = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifiers] options:nil];
  147. [[PHCachingImageManager defaultManager] requestImageDataForAsset:re.firstObject options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  148. UIImage * img = [UIImage imageWithData:imageData];
  149. if (img) {
  150. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  151. //将照片存入相册中
  152. UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
  153. }
  154. [self pushSelectSinglePhotoViewControllerWithImage:img controller:picker];
  155. }
  156. }];
  157. }
  158. }
  159. }
  160. - (void)pushSelectSinglePhotoViewControllerWithImage:(UIImage *)image controller:(UIImagePickerController *)picker {
  161. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  162. //将照片存入相册中
  163. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
  164. }
  165. SPSBSelectSinglePhotoViewController *selectSinglePhotoViewController = [[SPSBSelectSinglePhotoViewController alloc] init];
  166. selectSinglePhotoViewController.spsb_needToDismiss = true;
  167. selectSinglePhotoViewController.spsb_dataImage = image;
  168. selectSinglePhotoViewController.spsb_proportion = _proportion;
  169. @weakify(self)
  170. [selectSinglePhotoViewController passValue:^(NSValue *value) {
  171. @strongify(self)
  172. if (value) {
  173. self->_isOpenCamera = false;
  174. spsb_setIsOpenAlbum(false);
  175. self->_completion(image, value);
  176. } else {
  177. self->_completion(nil, nil);
  178. }
  179. }];
  180. [picker pushViewController:selectSinglePhotoViewController animated:true];
  181. }
  182. #pragma mark - SPSBNotificationDelegate
  183. - (void)spsb_shouldDismissAlbum:(void (^)(void))complete {
  184. if (spsb_isOpenAlbum()) {
  185. spsb_setIsOpenAlbum(false);
  186. [_myViewController dismissViewControllerAnimated:false completion:^{
  187. complete();
  188. }];
  189. }
  190. }
  191. @end