// // RQShareFunction.m // LN_School // // Created by 张嵘 on 2018/10/16. // Copyright © 2018 Danson. All rights reserved. // #import "RQShareFunction.h" #import "PureCamera.h"//第三方相机 #import "TZImagePickerController.h"//第三方相册 static RQShareFunction *_instance = nil; static dispatch_once_t onceToken; @implementation RQShareFunction + (instancetype)shareManager { return [[self alloc] init]; } - (instancetype)init{ dispatch_once(&onceToken, ^{ _instance = [super init]; }); return _instance; } #pragma mark - GetPhotos - (void)getPhotosWithGetPhotosWay:(GetPhotosWay)getPhotosWay size:(CGSize)size maxLength:(NSUInteger)maxLength maxImagesCount:(NSUInteger)maxImagesCount photosBlock:(PhotosBlock)photosBlock { if (getPhotosWay == GetPhotosWay_Camera) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { PureCamera *homec = [[PureCamera alloc] init]; homec.fininshcapture = ^(UIImage *photo) { if (photo) { NSMutableArray *imagesArr = [NSMutableArray arrayWithObject:photo]; NSData *data; if (CGSizeEqualToSize(size, CGSizeZero)) { data = [self compressQualityWithImage:photo MaxLength:maxLength]; }else { data = [self compressQualityWithImage:[photo scaledAndCutToSize:size] MaxLength:maxLength]; } NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSMutableArray *imagesDataStrArr = [NSMutableArray arrayWithObject:imgString]; if (photosBlock) { photosBlock(imagesArr, imagesDataStrArr); } } }; [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:homec animated:NO completion:nil]; } else { NSLog(@"相机调用失败"); } }else if (getPhotosWay == GetPhotosWay_Album) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount ? maxImagesCount : 1 delegate:nil]; imagePickerVc.naviBgColor = RQMianColor; imagePickerVc.iconThemeColor = RQMianColor; imagePickerVc.allowPickingVideo = NO; [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isStop) { NSMutableArray *imagesDataStrArr = [NSMutableArray array]; @synchronized (imagesDataStrArr) { for (UIImage *image in photos) { NSData *data; if (CGSizeEqualToSize(size, CGSizeZero)) { data = [self compressQualityWithImage:image MaxLength:maxLength]; }else { data = [self compressQualityWithImage:[image scaledAndCutToSize:size] MaxLength:maxLength]; } NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; [imagesDataStrArr addObject:imgString]; } } if (photosBlock) { photosBlock(photos, imagesDataStrArr); } }]; [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:imagePickerVc animated:YES completion:nil]; }else{ NSLog(@"相册调用失败"); } } } #pragma mark - RQAlertViewController - (void)showAlertAtViewController:(nonnull UIViewController *)viewController WithTitle:(nullable NSString *)title message:(nullable NSString *)message alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle cancelButtonTitle:(nonnull NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles otherButtonStyles:(nullable NSDictionary *)otherButtonStyles preferredActionTitle:(nullable NSString *)preferredActionTitle completion:(nullable RQAlertViewCompletion)completion { __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertControllerStyle]; void (^alertActionHandler) (UIAlertAction *) = [^(UIAlertAction *action) { if (completion) { if (action.style == UIAlertActionStyleCancel) { completion(NSNotFound); }else { NSUInteger index = [alertController.actions indexOfObject:action]; completion(index - 1); } } alertController = nil; } copy]; [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:alertActionHandler]]; @synchronized (alertController) { for (NSString *buttonTitle in otherButtonTitles) { NSNumber *actionStyleNumber = [otherButtonStyles valueForKey:buttonTitle]; UIAlertActionStyle actionStyle = UIAlertActionStyleDefault; if (actionStyleNumber) { actionStyle = [actionStyleNumber integerValue]; } UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle style:actionStyle handler:alertActionHandler]; [alertController addAction:action]; ///Support for iOS9 add preferredAction for highlights the text of that action if ([alertController respondsToSelector:@selector(setPreferredAction:)]) { if ([preferredActionTitle isEqualToString:buttonTitle]) { if (@available(iOS 9.0, *)) { [alertController setPreferredAction:action]; } else { // Fallback on earlier versions } } } } } [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:alertController animated:YES completion:nil]; } - (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle cancelButtonTitle:(nonnull NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles otherButtonStyles:(nullable NSDictionary *)otherButtonStyles preferredActionTitle:(nullable NSString *)preferredActionTitle completion:(nullable RQAlertViewCompletion)completion{ [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC] WithTitle:title message:message alertControllerStyle:alertControllerStyle cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles otherButtonStyles:otherButtonStyles preferredActionTitle:preferredActionTitle completion:completion]; } - (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle cancelButtonTitle:(nonnull NSString *)cancelButtonTitle otherButtonTitles:(nullable NSArray *)otherButtonTitles otherButtonStyles:(nullable NSDictionary *)otherButtonStyles completion:(nullable RQAlertViewCompletion)completion { [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC] WithTitle:title message:message alertControllerStyle:alertControllerStyle cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles otherButtonStyles:otherButtonStyles preferredActionTitle:nil completion:completion]; } #pragma mark - Custom Way - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication].delegate window]; //app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的 if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if (tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } id nextResponder = nil; UIViewController *appRootVC=window.rootViewController; // 如果是present上来的appRootVC.presentedViewController 不为nil if (appRootVC.presentedViewController) { //多层present while (appRootVC.presentedViewController) { appRootVC = appRootVC.presentedViewController; if (appRootVC) { nextResponder = appRootVC; }else{ break; } } // nextResponder = appRootVC.presentedViewController; }else{ // 如果当前UIViewController顶层不是UIView,那就需要循环获取到该UIViewController对应的UIView, // 才能跳出循环 int i= 0; UIView *frontView = [[window subviews] objectAtIndex:i]; nextResponder = [frontView nextResponder]; while (![nextResponder isKindOfClass:[UIViewController class]]) { i++; frontView = [[window subviews]objectAtIndex:i]; nextResponder = [frontView nextResponder]; } } if ([nextResponder isKindOfClass:[UITabBarController class]]){ UITabBarController * tabbar = (UITabBarController *)nextResponder; UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex]; // UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行 result=nav.childViewControllers.lastObject; }else if ([nextResponder isKindOfClass:[UINavigationController class]]){ UIViewController * nav = (UIViewController *)nextResponder; result = nav.childViewControllers.lastObject; }else{ result = nextResponder; } return result; } - (NSData *)compressQualityWithImage:(UIImage *)image MaxLength:(NSInteger)maxLength { CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(image, compression); if (data.length < maxLength) return data; CGFloat max = 1; CGFloat min = 0; NSData *lastData; for (int i = 0; i < 99; ++i) { compression = (max + min) / 2; data = UIImageJPEGRepresentation(image, compression); if (data.length == lastData.length) { return data; }else { lastData = data; } if (data.length < maxLength * 0.9) { min = compression; } else if (data.length > maxLength) { max = compression; } else { break; } } return data; } @end