RQShareFunction.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // RQShareFunction.m
  3. // LN_School
  4. //
  5. // Created by 张嵘 on 2018/10/16.
  6. // Copyright © 2018 Danson. All rights reserved.
  7. //
  8. #import "RQShareFunction.h"
  9. #import "PureCamera.h"//第三方相机
  10. #import "TZImagePickerController.h"//第三方相册
  11. static RQShareFunction *_instance = nil;
  12. static dispatch_once_t onceToken;
  13. @implementation RQShareFunction
  14. + (instancetype)shareManager {
  15. return [[self alloc] init];
  16. }
  17. - (instancetype)init{
  18. dispatch_once(&onceToken, ^{
  19. _instance = [super init];
  20. });
  21. return _instance;
  22. }
  23. #pragma mark - GetPhotos
  24. - (void)getPhotosWithGetPhotosWay:(GetPhotosWay)getPhotosWay size:(CGSize)size maxLength:(NSUInteger)maxLength maxImagesCount:(NSUInteger)maxImagesCount photosBlock:(PhotosBlock)photosBlock {
  25. if (getPhotosWay == GetPhotosWay_Camera) {
  26. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  27. PureCamera *homec = [[PureCamera alloc] init];
  28. homec.fininshcapture = ^(UIImage *photo) {
  29. if (photo) {
  30. NSMutableArray *imagesArr = [NSMutableArray arrayWithObject:photo];
  31. NSData *data;
  32. if (CGSizeEqualToSize(size, CGSizeZero)) {
  33. data = [self compressQualityWithImage:photo MaxLength:maxLength];
  34. }else {
  35. data = [self compressQualityWithImage:[photo scaledAndCutToSize:size] MaxLength:maxLength];
  36. }
  37. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  38. NSMutableArray *imagesDataStrArr = [NSMutableArray arrayWithObject:imgString];
  39. if (photosBlock) {
  40. photosBlock(imagesArr, imagesDataStrArr);
  41. }
  42. }
  43. };
  44. [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:homec
  45. animated:NO
  46. completion:nil];
  47. } else {
  48. NSLog(@"相机调用失败");
  49. }
  50. }else if (getPhotosWay == GetPhotosWay_Album) {
  51. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
  52. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount ? maxImagesCount : 1 delegate:nil];
  53. imagePickerVc.naviBgColor = RQMianColor;
  54. imagePickerVc.iconThemeColor = RQMianColor;
  55. imagePickerVc.allowPickingVideo = NO;
  56. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isStop) {
  57. NSMutableArray *imagesDataStrArr = [NSMutableArray array];
  58. @synchronized (imagesDataStrArr) {
  59. for (UIImage *image in photos) {
  60. NSData *data;
  61. if (CGSizeEqualToSize(size, CGSizeZero)) {
  62. data = [self compressQualityWithImage:image MaxLength:maxLength];
  63. }else {
  64. data = [self compressQualityWithImage:[image scaledAndCutToSize:size] MaxLength:maxLength];
  65. }
  66. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  67. [imagesDataStrArr addObject:imgString];
  68. }
  69. }
  70. if (photosBlock) {
  71. photosBlock(photos, imagesDataStrArr);
  72. }
  73. }];
  74. [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:imagePickerVc animated:YES completion:nil];
  75. }else{
  76. NSLog(@"相册调用失败");
  77. }
  78. }
  79. }
  80. #pragma mark - RQAlertViewController
  81. - (void)showAlertAtViewController:(nonnull UIViewController *)viewController
  82. WithTitle:(nullable NSString *)title
  83. message:(nullable NSString *)message
  84. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  85. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  86. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  87. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  88. preferredActionTitle:(nullable NSString *)preferredActionTitle
  89. completion:(nullable RQAlertViewCompletion)completion {
  90. __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertControllerStyle];
  91. void (^alertActionHandler) (UIAlertAction *) = [^(UIAlertAction *action) {
  92. if (completion) {
  93. if (action.style == UIAlertActionStyleCancel) {
  94. completion(NSNotFound);
  95. }else {
  96. NSUInteger index = [alertController.actions indexOfObject:action];
  97. completion(index - 1);
  98. }
  99. }
  100. alertController = nil;
  101. } copy];
  102. [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:alertActionHandler]];
  103. @synchronized (alertController) {
  104. for (NSString *buttonTitle in otherButtonTitles) {
  105. NSNumber *actionStyleNumber = [otherButtonStyles valueForKey:buttonTitle];
  106. UIAlertActionStyle actionStyle = UIAlertActionStyleDefault;
  107. if (actionStyleNumber) {
  108. actionStyle = [actionStyleNumber integerValue];
  109. }
  110. UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle
  111. style:actionStyle
  112. handler:alertActionHandler];
  113. [alertController addAction:action];
  114. ///Support for iOS9 add preferredAction for highlights the text of that action
  115. if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
  116. if ([preferredActionTitle isEqualToString:buttonTitle]) {
  117. if (@available(iOS 9.0, *)) {
  118. [alertController setPreferredAction:action];
  119. } else {
  120. // Fallback on earlier versions
  121. }
  122. }
  123. }
  124. }
  125. }
  126. [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:alertController animated:YES completion:nil];
  127. }
  128. - (void)showAlertWithTitle:(nullable NSString *)title
  129. message:(nullable NSString *)message
  130. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  131. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  132. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  133. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  134. preferredActionTitle:(nullable NSString *)preferredActionTitle
  135. completion:(nullable RQAlertViewCompletion)completion{
  136. [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC]
  137. WithTitle:title
  138. message:message
  139. alertControllerStyle:alertControllerStyle
  140. cancelButtonTitle:cancelButtonTitle
  141. otherButtonTitles:otherButtonTitles
  142. otherButtonStyles:otherButtonStyles
  143. preferredActionTitle:preferredActionTitle
  144. completion:completion];
  145. }
  146. - (void)showAlertWithTitle:(nullable NSString *)title
  147. message:(nullable NSString *)message
  148. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  149. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  150. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  151. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  152. completion:(nullable RQAlertViewCompletion)completion {
  153. [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC]
  154. WithTitle:title
  155. message:message
  156. alertControllerStyle:alertControllerStyle
  157. cancelButtonTitle:cancelButtonTitle
  158. otherButtonTitles:otherButtonTitles
  159. otherButtonStyles:otherButtonStyles
  160. preferredActionTitle:nil
  161. completion:completion];
  162. }
  163. #pragma mark - Custom Way
  164. - (UIViewController *)getCurrentVC {
  165. UIViewController *result = nil;
  166. UIWindow * window = [[UIApplication sharedApplication].delegate window];
  167. //app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的
  168. if (window.windowLevel != UIWindowLevelNormal)
  169. {
  170. NSArray *windows = [[UIApplication sharedApplication] windows];
  171. for(UIWindow * tmpWin in windows)
  172. {
  173. if (tmpWin.windowLevel == UIWindowLevelNormal)
  174. {
  175. window = tmpWin;
  176. break;
  177. }
  178. }
  179. }
  180. id nextResponder = nil;
  181. UIViewController *appRootVC=window.rootViewController;
  182. // 如果是present上来的appRootVC.presentedViewController 不为nil
  183. if (appRootVC.presentedViewController) {
  184. //多层present
  185. while (appRootVC.presentedViewController) {
  186. appRootVC = appRootVC.presentedViewController;
  187. if (appRootVC) {
  188. nextResponder = appRootVC;
  189. }else{
  190. break;
  191. }
  192. }
  193. // nextResponder = appRootVC.presentedViewController;
  194. }else{
  195. // 如果当前UIViewController顶层不是UIView,那就需要循环获取到该UIViewController对应的UIView,
  196. // 才能跳出循环
  197. int i= 0;
  198. UIView *frontView = [[window subviews] objectAtIndex:i];
  199. nextResponder = [frontView nextResponder];
  200. while (![nextResponder isKindOfClass:[UIViewController class]]) {
  201. i++;
  202. frontView = [[window subviews]objectAtIndex:i];
  203. nextResponder = [frontView nextResponder];
  204. }
  205. }
  206. if ([nextResponder isKindOfClass:[UITabBarController class]]){
  207. UITabBarController * tabbar = (UITabBarController *)nextResponder;
  208. UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
  209. // UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
  210. result=nav.childViewControllers.lastObject;
  211. }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
  212. UIViewController * nav = (UIViewController *)nextResponder;
  213. result = nav.childViewControllers.lastObject;
  214. }else{
  215. result = nextResponder;
  216. }
  217. return result;
  218. }
  219. - (NSData *)compressQualityWithImage:(UIImage *)image MaxLength:(NSInteger)maxLength {
  220. CGFloat compression = 1;
  221. NSData *data = UIImageJPEGRepresentation(image, compression);
  222. if (data.length < maxLength) return data;
  223. CGFloat max = 1;
  224. CGFloat min = 0;
  225. NSData *lastData;
  226. for (int i = 0; i < 99; ++i) {
  227. compression = (max + min) / 2;
  228. data = UIImageJPEGRepresentation(image, compression);
  229. if (data.length == lastData.length) {
  230. return data;
  231. }else {
  232. lastData = data;
  233. }
  234. if (data.length < maxLength * 0.9) {
  235. min = compression;
  236. } else if (data.length > maxLength) {
  237. max = compression;
  238. } else {
  239. break;
  240. }
  241. }
  242. return data;
  243. }
  244. @end