[一句秒懂]iOS调用相机和相册-细节化

1:首先遵照这两个代理:服务器

UIImagePickerControllerDelegateide

UINavigationControllerDelegate测试

#pragma mark - 调用相机 && 拍照

- (void)chooseImagePicker {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择照片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController = imagePickerController;
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        [self chooseAlbum];
    }];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        [self takePhoto];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action){}];
    
    // 弹出UIAlertAction窗口
    [self presentViewController:alertController animated:YES completion:nil];
    
    //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候咱们不须要把拍照功能加速
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        [alertController addAction:albumAction];
        [alertController addAction:photoAction];
        [alertController addAction:cancelAction];
    }else {
        
        [alertController addAction:albumAction];
        [alertController addAction:cancelAction];
    }
}

 

2:这是实现UIImagePickerControllerDelegate代理得到到的图片(相机选择和拍照的图片)ui

#pragma mark - UIImagePickerControllerDelegate代理

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    //选取裁剪后的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    // 上传图片
    [self upLoadImage:image];
}


#pragma mark - 取消选择照片

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    YLLog(@"取消");
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

 

3:上传图片:这个上传服务器的方法可能不同,可是内容大体同样spa

#pragma mark - 上传图像

- (void)upLoadImage:(UIImage *)image {
    [self hudShowLoad];

    YLUploadParam *uploadParam = [YLUploadParam new];
    uploadParam.data  = UIImageJPEGRepresentation(image, 1);
    uploadParam.name = @"file";
    uploadParam.fileName = @"avatar.png";
    uploadParam.mimeType = @"image/png";
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"method"] = @"user.setavatar";
    parameters[@"userid"] = self.uid;
    
    [YLHttpTool Upload:kApisetAvatar parameters:parameters uploadParam:uploadParam progress:^(id progress) {
    } success:^(id responseObject) {
        [self hudHide];
        if ([responseObject[@"status"] integerValue] == kHttpOk) {
            [self hudShowSuccess:@"上传成功"];
            YLLog(@"----%@",responseObject[@"data"]);
            self.model.avatar = responseObject[@"data"];
            NSIndexPath*index=[NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
        }else {
            [self hudCustom:responseObject[@"msg"] withIcon:nil];
        }
    } failure:^(NSError *error) {
        [self hudHide];
    } getFailure:^(NSString *error) {
        [self hudHide];
    }];

}

 

4:选择照片以前权限的判断代理

/**
 *  选择照片
 */

- (void)chooseAlbum {
    
    PHAuthorizationStatus albumStatus = [PHPhotoLibrary authorizationStatus];
    if (albumStatus == PHAuthorizationStatusDenied || albumStatus == PHAuthorizationStatusRestricted) { //用户限制和拒绝了(无权限)
        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        NSString *message = @"请求访问您的相册,请到设置->隐私->相册 -> 进行相应的受权";
        [self addAlertController:message];
    }else { // 有权限
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:_imagePickerController animated:YES completion:^{}];
    }
    
}

 

5:选择拍照照片以前的权限的判断code

/**
 *  拍照
 */
- (void)takePhoto {
    AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (videoStatus == AVAuthorizationStatusRestricted || videoStatus == AVAuthorizationStatusDenied) { // 未受权
        NSString *message = @"请求访问您的相机,请到设置->隐私->相机 -> 进行相应的受权";
        [self addAlertController:message];
    }else {  //受权了
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:_imagePickerController animated:YES completion:^{
        }];
    }
}

6:弹窗提示封装在一个方法里图片

/**
 *  权限提示框
 */
- (void)addAlertController:(NSString *)message {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertC addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]];
    [self presentViewController:alertC animated:YES completion:nil];
}

 

友情提示:get

在咱们连接手机真机测试或者模拟器测试的时候,若是咱们没有权限访问的时候,当咱们去手机或者模拟器设置-隐私-相机或者相册修改权限的时候,这时候系统会将程序从新启动,从而先杀死,引发表面的崩溃,可是这不是问题,当咱们在手机上测试,没有链接电脑程序的时候,就不会出现这样的问题,对于刚开始摸不到头绪的人来讲可能吓了一跳,哈哈,不要担忧了哈it

相关文章
相关标签/搜索