美文网首页
相机相册的正确打开方式

相机相册的正确打开方式

作者: CoderHG | 来源:发表于2017-04-02 22:14 被阅读151次

    ** 直接上代码: **

    导入

    #import <AVFoundation/AVFoundation.h>
    #import <Photos/Photos.h>
    

    一个金典的宏定义

    // 从 SDWebImage 中学来的
    #define dispatch_main_async_safe(block)\
        if ([NSThread isMainThread]) {\
            block();\
        } else {\
            dispatch_async(dispatch_get_main_queue(), block);\
        }
    

    打开相册的完整流程

    
    // 打开相册的完整流程
    - (void)openPhotosAlbum {
        // 进入相册
        void (^openBlock) () = ^{
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
                picker.sourceType    = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                [self presentViewController:picker animated:YES completion:nil];
            } else {
                NSLog(@"不能打开了");
            }
        };
        
        // 使用 PHPhotoLibrary
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            
            // 先定义一个block
            void (^photosBlock)() = ^{
                switch (status) {
                    case PHAuthorizationStatusNotDetermined:
                    {
                        // 没有确认过
                        NSLog(@"不处理");
                    }
                        break;
                    case PHAuthorizationStatusRestricted:
                    {
                        // 暂未使用
                        NSLog(@"被拒绝");
                    }
                        break;
                    case PHAuthorizationStatusDenied:
                    {
                        // 点击了不允许
                        NSLog(@"被否认");
                    }
                        break;
                    case PHAuthorizationStatusAuthorized:
                    {
                        // 同意
                        NSLog(@"可以 直接通过 PHFetchOptions 获取, 或者打开系统的相册");
                        openBlock();
                    }
                        break;
                        
                    default:
                        break;
                }
            };
            
            // 保证在主线程中获取相册中的图片
            dispatch_main_async_safe(photosBlock);
            
        }];
    }
    

    打开相机的完整流程(或者是仅仅获取相册中的图片)

    
    // 打开相机的完整流程
    - (void)openCamera {
        
        // 进入相机
        void (^openBlock) () = ^{
            UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
                picker.sourceType    = sourceType;
                [self presentViewController:picker animated:YES completion:nil];
            } else {
                NSLog(@"不能打开了");
            }
        };
        
        
        // 权限判断
        NSString *mediaType = AVMediaTypeVideo;
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        
        switch (status) {
            case AVAuthorizationStatusNotDetermined:
            {
                // 第一次访问
                NSLog(@"没有确认过");
                
                // 需要问一下,弹出是否同意提示框
                [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
                    
                    if(granted){
                        NSLog(@"同意");
                        // 进入相机
                        openBlock();
                    } else {
                        NSLog(@"不同意");
                        
                    }
                    
                }];
            }
                break;
            case AVAuthorizationStatusRestricted:
            {
                // (此状态,暂未使用)
                NSLog(@"被拒绝");
            }
                break;
            case AVAuthorizationStatusDenied:
            {
                NSLog(@"被否认过");
            }
                break;
            case AVAuthorizationStatusAuthorized:
            {
                // 允许访问
                NSLog(@"允许访问");
                
                // 进入相机
                openBlock();
            }
                break;
                
            default:
            {
                NSLog(@"未知状态");
            }
                break;
        }
        
        
    }
    

    代理 UINavigationControllerDelegate, UIImagePickerControllerDelegate

    #pragma mark -
    #pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
    {
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        
        NSLog(@"%@", image);
        
        [self imagePickerControllerDidCancel:picker];
    }
    
    // 取消操作
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    

    要记得设置下面两个东东

    ** Privacy - Camera Usage Description **
    ** Privacy - Photo Library Usage Description **

    忘记了也木有关系,反正会以 ** 闪退** 的方式来进行提示的.

    谢谢~

    相关文章

      网友评论

          本文标题:相机相册的正确打开方式

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