简述iOS修改头像功能
2016-11-28 本文已影响105人
75dfd512edfa
本文是为新手提供快速实现修改头像的功能,不为具体方法作赘诉
一.添加库
<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);
}];
}