美文网首页
简述iOS修改头像功能

简述iOS修改头像功能

作者: 75dfd512edfa | 来源:发表于2016-11-28 22:55 被阅读105次

本文是为新手提供快速实现修改头像的功能,不为具体方法作赘诉
一.添加库

<MobileCoreServices/MobileCoreServices.h>

二.添加代理

UIImagePickerControllerDelegate
UINavigationControllerDelegate
UIActionSheetDelegate(如果使用UIActionSheet则可添加此代理)

三.实现功能框

-(void)changeUserImage
{
    UIActionSheet *myAction=[[UIActionSheet alloc]init];
    
    //标题
    [myAction addButtonWithTitle:@"拍照"];
    [myAction addButtonWithTitle:@"从手机相册选择"];
    [myAction addButtonWithTitle:@"取消"];
    
    //设置标题
    myAction.delegate=self;
    
    //区分按钮
    myAction.cancelButtonIndex=2;
    
    [myAction showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",(long)buttonIndex);
    
    if (buttonIndex==0)
    {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [self loadImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
        }
        else
        {
            [self loadAlterViewWithString:@"不支持拍照功能"];
        }
    }
    
    else if (buttonIndex==1)//相册类型
    {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            [self loadImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        }
        else
        {
            [self loadAlterViewWithString:@"无法访问本地相册!"];
        }
        
    }
    
}
- (void)loadImagePickerWithSourceType:(int)type
{
    // 创建访问相机/图库/相册的视图控制器
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    // 设置访问的资源类型
    pickerController.sourceType = type;
    // 设置代理
    pickerController.delegate = self;
    //照片剪裁
    pickerController.allowsEditing=YES;
    // 跳转到我们需要访问的资源里面
    [self presentViewController:pickerController animated:YES completion:^{
        
    }];
}

四.实现资源代理方法

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //取消的时候返回到自己的视图控制器
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}
// 点击pickerController的choose按钮时被调用
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    /*
     UIImagePickerControllerMediaType-指定用户选中的媒体类型
     UIImagePickerControllerOriginalImage-原始图片
     UIImagePickerControllerEditedImage-修改后的图片
     UIImagePickerControllerCropRect-裁剪尺寸
     UIImagePickerControllerMediaURL-媒体的URL
     UIImagePickerControllerReferenceURL-原件的URL
     UIImagePickerControllerMediaMetadata-当来源是属于相机的时候这个值才有效
     */
    // 判断选中的资源类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    // kUTTypeImage-系统预置的图片资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
    {
        // 从我们的字典里面获取我们选中的图片
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
       // _headView.image=image; 给imageView赋值
       
        [self loadDataPohto:[UIImage imageWithData:imageData]];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

五.上传头像

-(void)loadDataPohto:(UIImage *)iconImg{
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
   
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html/",@"application/json",@"text/plain", nil];
    
    [session POST:HeadURL parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSData *data=UIImagePNGRepresentation(iconImg);
        
        [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%@",@"file.png"] mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//成功
        NSLog(@"PostJSON: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         //失败
        NSLog(@"PostError: %@", error);
    }];

}

相关文章

  • 简述iOS修改头像功能

    本文是为新手提供快速实现修改头像的功能,不为具体方法作赘诉一.添加库 二.添加代理 三.实现功能框 四.实现资源代...

  • iOS学习:调用相机和相册,选择图片上传

    今天在项目中要实现一个上传头像功能,点击修改头像行弹出拍照或者从相册选择来设置头像。 实现效果如下图: iOS有两...

  • 头像修改功能

    准备 在阿里云申请AccessKey ID、Access Key Secret,妥善保管 申请阿里云OSS,新建B...

  • 修改头像功能

    1.开发前准备 在阿里云申请AccessKey ID、Access Key Secret,妥善保管 申请阿里云OS...

  • 修改头像功能

    1.开发前准备 在阿里云申请AccessKey ID、Access Key Secret,妥善保管 申请阿里云OS...

  • 修改头像功能

    1 准备工作 在阿里云申请AccessKey ID、Access Key Secret,保存 申请阿里云OSS,新...

  • android 模仿微信头像裁剪

    在android开发中经常会碰到修改头像的功能需求, 而在修改头像>中最难的部分就是在头像的裁剪功能。虽说网上一大...

  • Glide如何加载同一URL下的最新图片

    简述 基于项目需求,用户更换新头像后,iOS、Android、web 端三端需要能更新到最新的头像。由于各种原因,...

  • iOS Download Image In The Same U

    简述 基于项目需求,上传头像成功后,iOS、Android、web端三端需要实时更新到最新的头像。 考虑到以上需求...

  • iOS11 控制中心关闭蓝牙仅断开设备,Bluetooth断开设

    简述 iOS 11 对控制中心的功能做了修改,蓝牙、wifi模块在关闭按钮时仅是断开设备,并不是真正的关闭,很...

网友评论

      本文标题:简述iOS修改头像功能

      本文链接:https://www.haomeiwen.com/subject/kdfapttx.html