美文网首页
PhotoPickManager

PhotoPickManager

作者: 阿龍飛 | 来源:发表于2020-06-19 15:03 被阅读0次

    照片拍照功能

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef NS_ENUM(NSInteger, PickerType)
    {
        PickerType_Camera = 0, // 拍照
        PickerType_Photo, // 照片
    };
    
    typedef void(^CallBackBlock)(NSDictionary *infoDict, BOOL isCancel);  // 回调
    
    @interface PhotoPickManager : NSObject
    
    + (instancetype)shareInstance; // 单例
    
    - (void)presentPicker:(PickerType)pickerType target:(UIViewController *)vc callBackBlock:(CallBackBlock)callBackBlock;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "PhotoPickManager.h"
    
    @interface PhotoPickManager ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
    
    @property (nonatomic,strong) UIImagePickerController * imgPickC;
    
    @property (nonatomic,strong) UIViewController * vc;
    
    @property (nonatomic,copy) CallBackBlock  callBackBlock;
    
    @end
    
    @implementation PhotoPickManager
    
    + (instancetype)shareInstance
    {
        static dispatch_once_t once;
        static PhotoPickManager *pickManager = nil;
        dispatch_once(&once, ^{
            pickManager = [[PhotoPickManager alloc] init];
        });
        return pickManager;
    }
    
    - (instancetype)init
    {
        if([super init]){
            if(!self.imgPickC){
                self.imgPickC = [[UIImagePickerController alloc] init];
            }
        }
        return self;
    }
    
    - (void)presentPicker:(PickerType)pickerType target:(UIViewController *)vc callBackBlock:(CallBackBlock)callBackBlock
    {
        self.vc = vc;
        self.callBackBlock = callBackBlock;
        if(pickerType == PickerType_Camera)
        {
            // 拍照
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                self.imgPickC.delegate = self;
                self.imgPickC.sourceType = UIImagePickerControllerSourceTypeCamera;
                self.imgPickC.allowsEditing = YES;
                self.imgPickC.showsCameraControls = YES;
                UIView *view = [[UIView  alloc] init];
                view.backgroundColor = [UIColor grayColor];
                self.imgPickC.cameraOverlayView = view;
                [self.vc presentViewController:self.imgPickC animated:YES completion:nil];
            }else{
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"取消");
                }];
                [alertVc addAction:cancelBtn];
                [self.vc presentViewController:alertVc animated:YES completion:nil];
            }
        }
        
        else if(pickerType == PickerType_Photo)
        {
            // 相册
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
                self.imgPickC.delegate = self;
                self.imgPickC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                self.imgPickC.allowsEditing = YES;
                [self.vc presentViewController:self.imgPickC animated:YES completion:nil];
            }else{
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"" message:@"相册不可用" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"取消");
                }];
                [alertVc addAction:cancelBtn];
                [self.vc presentViewController:alertVc animated:YES completion:nil];
            }
        }
    }
    
    
    #pragma mark ---- UIImagePickerControllerDelegate
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
    {
        [self.vc dismissViewControllerAnimated:YES completion:^{
            self.callBackBlock(info, NO);
        }];
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self.vc dismissViewControllerAnimated:YES completion:^{
            self.callBackBlock(nil, YES);
        }];
    }
    
    @end
    

    调用方法

     __weak __typeof(self)weakSelf = self;
    PhotoPickManager *pickManager = [PhotoPickManager shareInstance];
    [pickManager presentPicker:PickerType_Photo
                        target:weakSelf
                 callBackBlock:^(NSDictionary *infoDict, BOOL isCancel) {
        //                _imgView.image = [infoDict valueForKey:UIImagePickerControllerOriginalImage];
    }];
    

    相关文章

      网友评论

          本文标题:PhotoPickManager

          本文链接:https://www.haomeiwen.com/subject/jawvxktx.html