iOS相册管理

iOS相册访问

  • 相关框架
#import 
#import 
  • 相册权限管理
    //获取相册访问权限
    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 *)info{
    //获取图片信息
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//取消事件代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

你可能感兴趣的:(iOS相册管理)