[一句秒懂]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

相關文章
相關標籤/搜索