LYLPhotoTailoringTool.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // LYLPhotoTailoringTool.m
  3. // PhotoTailoring
  4. //
  5. // Created by Rainy on 2018/8/22.
  6. // Copyright © 2018年 Rainy. All rights reserved.
  7. //
  8. #import "LYLPhotoTailoringTool.h"
  9. #import "LYLPhotoTailoringViewController.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. #import <Photos/Photos.h>
  12. @interface LYLPhotoTailoringTool ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,PhotoViewControllerDelegate>
  13. {
  14. ChoosImageBlock _choosImageBlock;
  15. ShowImageBlock _showImageBlock;
  16. UIImagePickerControllerSourceType _photoType;
  17. BOOL _crop;
  18. }
  19. @end
  20. @implementation LYLPhotoTailoringTool
  21. static LYLPhotoTailoringTool *tool = nil;
  22. + (instancetype)sharedTool
  23. {
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. tool = [LYLPhotoTailoringTool new];
  27. });
  28. return tool;
  29. }
  30. //- (void)photoTailoring:(ChoosImageBlock)finished
  31. - (void)photoTailoringAndShowImg:(NSString *)imgUrl showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock
  32. {
  33. _crop=YES;
  34. _choosImageBlock = choosImgBlock;
  35. _showImageBlock = showImgBlock;
  36. UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
  37. if(imgUrl){
  38. [alertController addAction:[UIAlertAction actionWithTitle:@"查看大图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  39. _showImageBlock(imgUrl);
  40. }]];
  41. }
  42. [alertController addAction:[UIAlertAction actionWithTitle:@"拍一张" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  43. [self choosePhoto:NO];
  44. }]];
  45. [alertController addAction:[UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  46. [self choosePhoto:YES];
  47. }]];
  48. [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  49. }]];
  50. [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil];
  51. }
  52. - (void)photoTailoringAndShowImg:(NSString *)imgUrl crop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock
  53. {
  54. _crop=crop;
  55. _choosImageBlock = choosImgBlock;
  56. _showImageBlock = showImgBlock;
  57. UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
  58. if(imgUrl){
  59. [alertController addAction:[UIAlertAction actionWithTitle:@"查看大图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  60. _showImageBlock(imgUrl);
  61. }]];
  62. }
  63. [alertController addAction:[UIAlertAction actionWithTitle:@"拍一张" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  64. [self choosePhoto:NO];
  65. }]];
  66. [alertController addAction:[UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  67. [self choosePhoto:YES];
  68. }]];
  69. [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  70. }]];
  71. [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil];
  72. }
  73. - (void)selectPhotoWithPhoroOrCamera:(SelectPhotoWithTypePhoroOrCamera)type crop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock
  74. {
  75. _crop=crop;
  76. _choosImageBlock = choosImgBlock;
  77. _showImageBlock = showImgBlock;
  78. if(type == SelectPhotoWithTypePhoroOrCamera1){
  79. [self choosePhoto:YES];
  80. }else if (type == SelectPhotoWithTypePhoroOrCamera2){
  81. [self choosePhoto:NO];
  82. }
  83. }
  84. //直接打开相册
  85. - (void)openPhoroWithCrop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock{
  86. _crop=crop;
  87. _choosImageBlock = choosImgBlock;
  88. _showImageBlock = showImgBlock;
  89. [self choosePhoto:YES];
  90. }
  91. //直接打开相机
  92. - (void)openCameraWithCrop:(BOOL)crop showImgBlock:(ShowImageBlock)showImgBlock choosImgBlock:(ChoosImageBlock)choosImgBlock{
  93. _crop=crop;
  94. _choosImageBlock = choosImgBlock;
  95. _showImageBlock = showImgBlock;
  96. [self choosePhoto:NO];
  97. }
  98. - (void)chooseImage:(ChoosImageBlock)choosImgBlock
  99. {
  100. _choosImageBlock = choosImgBlock;
  101. [self choosePhoto:YES];
  102. }
  103. - (void)choosePhoto:(BOOL)photo
  104. {
  105. WS(weakSelf)
  106. __block UIImagePickerControllerSourceType block_photoType = _photoType;
  107. [self checkCheckPermissionsType:(photo ? CheckPermissionsTypePhoto : CheckPermissionsTypeVideo) permissionGranted:^{
  108. UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
  109. ipc.sourceType = photo ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
  110. block_photoType = ipc.sourceType;
  111. ipc.delegate = weakSelf;
  112. ipc.allowsEditing = NO;
  113. ipc.modalTransitionStyle = [self getCurrentViewController].navigationController.modalTransitionStyle;
  114. ipc.modalPresentationStyle = [UtilsTools modalPresentationStyle:[self getCurrentViewController].navigationController.modalPresentationStyle];
  115. // [ipc.navigationBar setBarStyle:UIBarStyleBlack];
  116. // UIViewController * vc = [[UIViewController alloc] init];
  117. // [vc.view addSubview:ipc.view];
  118. // UIButton * btn = [UIButton new];
  119. // [btn setTitle:@"取消" forState:UIControlStateNormal];
  120. // btn.titleLabel.font = [UIFont systemFontOfSize:14];
  121. // [vc.view addSubview:btn];
  122. // [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  123. // make.top.mas_offset(IS_IPHONEX ? 44 : 20);
  124. // make.size.mas_offset(CGSizeMake(60, 44));
  125. // make.right.mas_offset(-10);
  126. // }];
  127. // [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  128. // [btn setAction:^{
  129. // [vc dismissViewControllerAnimated:YES completion:^{
  130. //
  131. // }];
  132. // }];
  133. [[self getCurrentViewController] presentViewController:ipc animated:YES completion:nil];
  134. } noPermission:^(CheckPermissionsType type) {
  135. NSString *message = @"";
  136. if (type == CheckPermissionsTypePhoto) {
  137. message = @"请在设置中,允许“灵犀办公”访问您的照片";
  138. }
  139. if (type == CheckPermissionsTypeVideo) {
  140. message = @"请在设置中,允许“灵犀办公”访问您的相机";
  141. }
  142. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
  143. [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  144. }]];
  145. [alertController addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  146. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  147. if ([[UIApplication sharedApplication]canOpenURL:url]) {
  148. [[UIApplication sharedApplication]openURL:url];
  149. }
  150. }]];
  151. [[self getCurrentViewController] presentViewController:alertController animated:YES completion:nil];
  152. }];
  153. }
  154. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  155. {
  156. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  157. if (_crop==NO) {
  158. _choosImageBlock(image);
  159. [picker dismissViewControllerAnimated:YES completion:nil];
  160. return;
  161. }
  162. LYLPhotoTailoringViewController *photoVC = [[LYLPhotoTailoringViewController alloc] init];
  163. photoVC.oldImage = image;
  164. photoVC.mode = PhotoMaskViewModeSquare;
  165. photoVC.cropWidth = CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds);
  166. photoVC.cropHeight = CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds);
  167. photoVC.delegate = self;
  168. photoVC.lineColor = [UIColor clearColor];
  169. [picker pushViewController:photoVC animated:YES];
  170. // if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  171. // UIImageWriteToSavedPhotosAlbum(image, self, nil, NULL);
  172. // }
  173. }
  174. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  175. [picker dismissViewControllerAnimated:YES completion:nil];
  176. }
  177. #pragma mark - photoViewControllerDelegate
  178. - (void)imageCropperDidCancel:(LYLPhotoTailoringViewController *)cropperViewController
  179. {
  180. if (_photoType == UIImagePickerControllerSourceTypePhotoLibrary) {
  181. [cropperViewController dismissViewControllerAnimated:YES completion:nil];
  182. }else{
  183. [cropperViewController dismissViewControllerAnimated:YES completion:nil];
  184. }
  185. }
  186. - (void)imageCropper:(LYLPhotoTailoringViewController *)cropperViewController didFinished:(UIImage *)editedImage
  187. {
  188. __weak ChoosImageBlock block_choosImageBlock = _choosImageBlock;
  189. [cropperViewController dismissViewControllerAnimated:YES completion:^{
  190. block_choosImageBlock(editedImage);
  191. }];
  192. }
  193. - (UIViewController *)getCurrentViewController {
  194. __block UIWindow *normalWindow = [[UIApplication sharedApplication].delegate window];
  195. if (normalWindow.windowLevel != UIWindowLevelNormal) {
  196. [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  197. if (obj.windowLevel == UIWindowLevelNormal) {
  198. normalWindow = obj;
  199. *stop = YES;
  200. }
  201. }];
  202. }
  203. return [self nextTopForViewController:normalWindow.rootViewController];
  204. }
  205. - (UIViewController *)nextTopForViewController:(UIViewController *)inViewController {
  206. while (inViewController.presentedViewController) {
  207. inViewController = inViewController.presentedViewController;
  208. }
  209. if ([inViewController isKindOfClass:[UITabBarController class]]) {
  210. UIViewController *selectedVC = [self nextTopForViewController:((UITabBarController *)inViewController).selectedViewController];
  211. return selectedVC;
  212. } else if ([inViewController isKindOfClass:[UINavigationController class]]) {
  213. UIViewController *selectedVC = [self nextTopForViewController:((UINavigationController *)inViewController).visibleViewController];
  214. return selectedVC;
  215. } else {
  216. return inViewController;
  217. }
  218. }
  219. typedef NS_ENUM(NSUInteger, CheckPermissionsType) {
  220. CheckPermissionsTypePhoto,
  221. CheckPermissionsTypeVideo,
  222. };
  223. - (void)checkCheckPermissionsType:(CheckPermissionsType)type
  224. permissionGranted:(void (^)(void))permissionGranted
  225. noPermission:(void (^)(CheckPermissionsType type))noPermission
  226. {
  227. if (type == CheckPermissionsTypePhoto)
  228. {
  229. PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
  230. if (photoAuthStatus == PHAuthorizationStatusNotDetermined) {
  231. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  232. dispatch_async(dispatch_get_main_queue(), ^{
  233. status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission(type);
  234. });
  235. }];
  236. }else if (photoAuthStatus == PHAuthorizationStatusRestricted || photoAuthStatus == PHAuthorizationStatusDenied){
  237. noPermission(type);
  238. }else
  239. {
  240. permissionGranted();
  241. }
  242. }
  243. if (type == CheckPermissionsTypeVideo) {
  244. AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  245. if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {
  246. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  247. dispatch_async(dispatch_get_main_queue(), ^{
  248. granted ? permissionGranted() : noPermission(type);
  249. });
  250. }];
  251. }else if (videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied){
  252. noPermission(type);
  253. }else
  254. {
  255. permissionGranted();
  256. }
  257. }
  258. }
  259. @end