26、[ iOS ] 头像上传

在此之前我们要先实现这两个代理协议

然后要导入 MBProgressHUD 这个第三方。

/**
 * 功能:头像上传
 */
    // ------系统相册
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing                  = YES;
    imagePicker.allowsEditing            = YES;
    imagePicker.delegate                 = self;
   
    UIAlertController *alertController     = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    // ------拍照
    UIAlertAction *PicAction     = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        // ------判断相机是否可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            imagePicker.sourceType  = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
       
        }else {
            
            MBProgressHUD *mbHUD   = [[MBProgressHUD alloc] initWithView:self.view];
            mbHUD.mode             = MBProgressHUDModeText;
            mbHUD.labelText        = @"该相机不可使用";
            [mbHUD show:YES];
            [mbHUD hide:YES afterDelay:1];
            [self.view addSubview:mbHUD];
        }
    }];
    
    // ------从手机相册选择
    UIAlertAction *PhotoAction   = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        imagePicker.sourceType   = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }];
    // ------取消
    UIAlertAction *cancelAction  = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:PicAction];
    [alertController addAction:PhotoAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
 
}

#pragma mark - **************** UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    // ------原始图片
    UIImage *OriginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
   
    // ------编辑后的图片
//    UIImage *editImage = [info objectForKey:UIImagePickerControllerEditedImage];
    
    // ------上传操作
    
}
注意:这里的头像上传并不是指上传到服务器。

你可能感兴趣的:(26、[ iOS ] 头像上传)