本地相册选择照片或相机拍照 获取当前Window最上面的 UIViewController

.h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface JMChooseImageHelper : NSObject


@property (assign, nonatomic) BOOL allowsEditing;

@property (assign, nonatomic) BOOL onlyUseCamera;   //  default NO

@property (assign, nonatomic) BOOL onlyUsePhoto;    //  default NO



@property (nonatomic, strong) UIImagePickerController *imagePicker;   //  选完照片后附上,close 时,置 nil

@property (nonatomic, strong) UIImage *originalImage;     //  选完照片后附上,close 时,置 nil

@property (nonatomic, strong) UIImage *editedImage;       //  选完照片后附上,close 时,置 nil



@property (nonatomic, copy) void(^resultImage)(JMChooseImageHelper *helper, NSDictionary *info);



+ (JMChooseImageHelper *)shared;


- (void)show;

- (void)close;


@end


.m


#import "JMChooseImageHelper.h"


@interface JMChooseImageHelper () <UIActionSheetDelegate>


@property (nonatomic, assign) UIStatusBarStyle currentStatusBarStyle;


@end;


@implementation JMChooseImageHelper

{

    UIActionSheet *_actionSheet;

}


+ (JMChooseImageHelper *)shared {

    static id obj;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        obj = [[self alloc] init];

    });

    return obj;

}


- (void)dealloc {

    NSLog(@"JMChooseImageHelper dealloc");

}


#pragma mark - show

- (void)show {

    

    

    if (_onlyUseCamera) {

        [JMChooseImageHelper openCamera];

        return;

    }

    if (_onlyUsePhoto) {

        [JMChooseImageHelper openPhoto];

        return;

    }

    

    [self.actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

}


- (void)close {

    if (self.imagePicker) {

        [self.imagePicker dismissViewControllerAnimated:YES completion:nil];

        

        self.originalImage = nil;

        self.editedImage = nil;

        self.imagePicker = nil;

    }

}


#pragma mark - auth

- (BOOL)checkPermissions {

    //dispatch_async(dispatch_get_main_queue(), ^{

    //    [[QMAlertViewTwo(@"没有权限使用相机", @"设置->隐私->相机", lls(@"取消"), lls(@"确定"))

    //      setCompleteBlock:^(UIAlertView *alertView, NSInteger index) {

    //          [self closeCamera];

    //          if (index != kAlertCancelIndex) {

    //              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

    //          }

    //      }] show];

    //    

    //});

    return YES;

}



#pragma mark - 菜单

// TODO

- (UIActionSheet *)actionSheet {

    

    if (_actionSheet == nil) {

        NSString *openCamera = NSLocalizedString(@"拍照", nil);

        NSString *openPhoto = NSLocalizedString(@"从手机相册选择", nil);

        NSString *cancel = NSLocalizedString(@"取消", nil);

        

//        _actionSheet = [[UIActionSheet alloc] init];

//        [_actionSheet addButtonWithTitle:openCamera];

//        [_actionSheet addButtonWithTitle:openPhoto];

//        [_actionSheet addButtonWithTitle:cancel];

//        _actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

//        _actionSheet.delegate = self;

        

        

        _actionSheet = [[UIActionSheet alloc] initWithTitle:nil

                                                   delegate:self

                                          cancelButtonTitle:cancel

                                     destructiveButtonTitle:nil

                                          otherButtonTitles:openCamera, openPhoto, nil];

        

    }

    return _actionSheet;

}


- (void)clickEmpty {

    [self.actionSheet dismissWithClickedButtonIndex:2 animated:YES];

}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    

    if (buttonIndex == 0) {

        [JMChooseImageHelper openCamera];

        return;

    }

    if (buttonIndex == 1) {

        [JMChooseImageHelper openPhoto];

        return;

    }

}


/**

 *  @return 当前Window最上面的 UIViewController

 */

+ (UIViewController *)currentWindowTopViewController {

    

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    

    //  normal 才是正确的 window

    if (window.windowLevel != UIWindowLevelNormal) {

        

        NSArray *windows = [[UIApplication sharedApplication] windows];

        

        for (UIWindow * win in windows) {

            if (win.windowLevel == UIWindowLevelNormal) {

                window = win;

                break;

            }

        }

    }

    

    UIView *frontView = [[window subviews] objectAtIndex:0];

    UIResponder *next = frontView.nextResponder;

    do {

        if ([next isKindOfClass:[UIViewController class]]) {

            return (UIViewController *)next;

        }

        next = next.nextResponder;

        

    } while(next != nil);

    

    return window.rootViewController;

}


+ (void)openPhoto {

    

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        

        [self shared].currentStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;

        

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];

        picker.delegate = [self shared];

        picker.allowsEditing = [self shared].allowsEditing;

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;


        [[self currentWindowTopViewController] presentViewController:picker animated:YES completion:nil];

    } else {

        [[[UIAlertView alloc] initWithTitle:@"没有权限" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];

    }

}


+ (void)openCamera {

    

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        

        [self shared].currentStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;

        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            UIImagePickerController *picker = [[UIImagePickerController alloc] init];

            picker.delegate = [self shared];

            picker.allowsEditing = [self shared].allowsEditing;

            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

            //        picker.showsCameraControls = NO;

            [[self currentWindowTopViewController] presentViewController:picker animated:YES completion:nil];

        });

    }else {

        

        [JMChooseImageHelper openPhoto];

    }

}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    self.imagePicker = picker;

    self.originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];//    UIImagePickerControllerMediaURL

    self.editedImage   = [info objectForKey:UIImagePickerControllerEditedImage];

    

    

    if (self.resultImage) {

        self.resultImage(self, info);

    }

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    self.imagePicker = picker;

    if (self.resultImage) {

        self.resultImage(self, nil);

    }

}


-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    

    [[UIApplication sharedApplication] setStatusBarStyle:self.currentStatusBarStyle];

}


@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值