IOS开发之—— 上传头像的使用

时间:2023-03-09 08:04:16
IOS开发之—— 上传头像的使用

static NSString *const uploadSuccess = @"更改头像成功";

@interface DMAccountInformationViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,AVAudioPlayerDelegate>

@property (nonatomic, strong) UIImageView *pictureImageView;    //头像

@property (nonatomic, strong) UIImage  *changeImage;

#pragma mark - ---------------- 事件 ------------------
#pragma mark - 选择照片
- (void)choosePhotoAction
{
    UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选取", nil];
    [actionSheet showInView:self.view];
}
#pragma mark -actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    if (buttonIndex==0) {
        
        NSString * mediaType = AVMediaTypeVideo;
        AVAuthorizationStatus  authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) {
            if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
                
                UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"摄像头访问受限" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertC animated:YES completion:nil];
                UIAlertAction * action = [UIAlertAction  actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertC addAction:action];
            }else
            {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"摄像头访问受限" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alertView show];
                
            }
            
        }else{
            
        }
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.allowsEditing = YES;
            imagePicker.delegate = self;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该设备相机不能使用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
    }else if (buttonIndex==1)
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.allowsEditing = YES;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

#pragma mark - ---------------- 请求 ------------------
#pragma mark - 上传图片请求
- (void)uploadImgRequestWithPatameters:(NSDictionary *)parameters
{
    [DMHttpRequest postUploadFile:DMUploadIconUrl parameters:parameters success:^(id responseObj) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingAllowFragments error:nil];
        DMRequestCode code = [DMTools responseCode:dictionary];
        NSLog(@"!!%@!!",dictionary);
        switch (code) {
            case DMRequestSuccess:
            {
                NSLog(@"!!%@!!",dictionary);
                [DMTools loadSuccessHUD:self.hud text:uploadSuccess delay:DMHUDDelayTimeInterval];
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSDictionary *imageDic = [dictionary objectForKey:@"data"];
                    NSString *imageStr = [imageDic objectForKey:@"url"];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        [DMUserLogInfoModel shareInstance].userData.avatar = imageStr;
                        
                        [DMUserDataManager saveUserData:[DMUserLogInfoModel shareInstance].userData.keyValues];
              
                        _pictureImageView.image = _changeImage;
                    });
                });
                
                
dispatch_get_main_queue(), ^{
            
                break;
            }
            default:
                [DMTools loadFailedHUD:self.hud text:[DMTools responseMessage:dictionary] delay:DMHUDDelayTimeInterval];

break;
        }
    } failure:^(NSError *error) {
        [DMTools loadFailedHUD:self.hud text:DMRequestFailureNote delay:DMHUDDelayTimeInterval];

}];
}