iOS相册管理

iOS相册访问

  • 相关框架
#import <AVFoundation/AVFoundation.h>
#import <Photos/PHPhotoLibrary.h>
  • 相册权限管理
    //获取相册访问权限
    PHAuthorizationStatus authorization = [PHPhotoLibrary authorizationStatus];
    if (authorization == PHAuthorizationStatusDenied) {
       //修改设置当中权限
       NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
        
    }else if(authorization == PHAuthorizationStatusNotDetermined){
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if(status == PHAuthorizationStatusAuthorized) {
                [self presentImageControllerWithType:ImagePickerTypePhoto];
            }
        }];
    }else if (authorization == PHAuthorizationStatusAuthorized) {
        [self presentImageControllerWithType:ImagePickerTypePhoto];
    }
  • 摄像头权限管理
-(void)openCamera{
    SKLog(@"openCamera");
    AVAuthorizationStatus authorization = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authorization == AVAuthorizationStatusDenied) {
       //修改设置当中权限
       NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
    }else if (authorization == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                [self presentImageControllerWithType:ImagePickerTypeCamera];
            }
        }];
    }else if (authorization == AVAuthorizationStatusAuthorized){
        [self presentImageControllerWithType:ImagePickerTypeCamera];
    }
}

  • 获取权限后调用UIImagePickerController
-(void)presentImageControllerWithType:(ImagePickerType)pickerType{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.view.backgroundColor = [UIColor whiteColor];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    switch (pickerType) {
        //判断当前相册是否可用
        case ImagePickerTypePhoto:{
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self.navigationController presentViewController:imagePicker animated:YES completion:^{
                }];
            }else{
                [MBProgressHUD showError:@"抱歉,无法访问相册"];
            }
        }
            break;
        //判断摄像头是否可用
        case ImagePickerTypeCamera:{
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
                if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {//后置摄像头可用
                    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
                    [self.navigationController presentViewController:imagePicker animated:YES completion:^{
                    }];
                }else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){//前置摄像头可用
                    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
                    [self.navigationController presentViewController:imagePicker animated:YES completion:^{
                    }];
                }else{
                    [MBProgressHUD showError:@"前后摄像头已损坏,无法使用"];
                }
            }else{
                [MBProgressHUD showError:@"抱歉,相机无法使用"];
            }
        }
            break;
        default:
            break;
    }
}
  • UIImagePickerController delegate
//图片选择代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取图片信息
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//取消事件代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 今天我们架子鼓要考级,老师说是下午一点开口,但是我们怕找不到地方,所以我们11:30就出发了。我们到了以后,老师说...
    小溪流18王家硕阅读 787评论 1 2
  • 当2016年除夕钟声敲响的时候,我很认真的回顾了整个2016年,发现竟然没有印象特别深刻有意义的事情,整整一年过去...
    程影阅读 463评论 0 3
  • 天色不足以痛哭 雨水业已哽咽 窗外的铁栏杆 停着一群白色的乌鸦 倒影中的荒原 无需语言
    书下的男爵阅读 249评论 0 2