zoukankan      html  css  js  c++  java
  • 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];

        }];
    }

  • 相关阅读:
    【Python】python3.6中实现同一行动态输出
    【Python】将python3.6软件的py文件打包成exe程序
    【Java】分布式RPC通信框架Apache Thrift 使用总结
    【Java】加载驱动方法
    【Java】Comparable和Comparator接口的区别
    【Java】接口(interface)VS抽象类
    【Ubuntu】PHP环境安装-phpstudy for linux版
    【Ubuntu】xrdp完美实现Windows远程访问Ubuntu 16.04
    【系统设计】会议室预定系统房间预定系统设计
    jmeter场景设计:业务占比
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5389926.html
Copyright © 2011-2022 走看看