123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // 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<UIImage *> *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
|