相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传
Posted 宁静暖风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传相关的知识,希望对你有一定的参考价值。
一、点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传
(1)上传头像属性
// 图片二进制格式 表单上传 @property (nonatomic, strong) NSData *imageWithData;
(2)头像点击事件
- (void)headImageEvent{ NSLog(@"上传头像"); [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) { self.headerImageView.image = img; NSData *dataWithImage; dataWithImage = UIImageJPEGRepresentation(img, 0.3); self.imageWithData = dataWithImage; }]; }
(3)打开相册或者拍照
/** 弹出提示框 选择相机或者相册 @param selectPhotoHandle 选中或拍摄的图片 */ - (void)selectPhotoAlbumWithSelectPhotoHandle:(void (^)(UIImage *))selectPhotoHandle{ self.selectPhotoHandle = selectPhotoHandle; UIAlertController *av = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self takePhoto]; }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self LocalPhoto]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [av addAction:action1]; [av addAction:action2]; [av addAction:cancelAction]; [self presentViewController:av animated:YES completion:nil]; } //开始拍照 -(void)takePhoto { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //设置拍照后的图片可被编辑 picker.allowsEditing = YES; picker.sourceType = sourceType; [self presentViewController:picker animated:YES completion:nil]; }else{ NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } } //打开本地相册 -(void)LocalPhoto{ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //设置选择后的图片可被编辑 picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; } //当选择一张图片后进入这里 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; if (self.selectPhotoHandle) { self.selectPhotoHandle(image); } [picker dismissViewControllerAnimated:YES completion:nil]; }
(4)网络上传头像
- (void)postUserHeaderImage { NSMutableDictionary *parameter = [[NSMutableDictionary alloc]init]; [parameter setValue:userToken forKey:@"userId"]; [parameter setValue:self.nickeNameTextField.text forKey:@"nickName"]; [[[NetRequest alloc]init]postRequestWithAPINameForData:@"/robot/userController/updateRecord.do" parameters:parameter imageWithData:self.imageWithData imageWithNameFile:@"portrait" NetRequestProgress:nil NetRequestSuccess:^(NSInteger code, NSString *msg, id response) { ShowMessage(@"编辑资料成功"); } NetRequestFaile:^(NSInteger code, NSString *msg, id response) { ShowMessage(@"获取网络数据失败"); }]; }
(5)上传头像方法
// 以流文件方式上传图片(表单上传) - (void)postRequestWithAPINameForData:(NSString *)api parameters:(NSDictionary *)param imageWithData:(NSData *)fileData imageWithNameFile:(NSString *)nameFile NetRequestProgress:(NetRequestProgress)progress NetRequestSuccess:(NetRequestSuccess)sucess NetRequestFaile:(NetRequestFaile)faile { // 请求地址 NSString *urlString = [NSString stringWithFormat:@"%@/%@", baseUrl, api]; // 设置状态栏网络访问的风火轮 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 网络请求Session AFHTTPSessionManager* session = [AFHTTPSessionManager manager]; session.requestSerializer.timeoutInterval = 15.f; session.responseSerializer.acceptableContentTypes = [session.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; // session.responseSerializer = [AFHTTPResponseSerializer serializer]; // 参数初始化 NSMutableDictionary *dicParam; if (param) { dicParam = [NSMutableDictionary dictionaryWithDictionary:param]; } else { dicParam = [NSMutableDictionary dictionary]; } // [dicParam setObject:@"i" forKey:@"p"]; // 平台 // [dicParam setObject:@"1.0.0" forKey:@"v"]; // 版本 [session POST:urlString parameters:dicParam constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { if (fileData.length > 0) { NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; // 注意 img 是后台给的流文件名 一定对应接口否则上传失败 [formData appendPartWithFileData:fileData name:nameFile fileName:[NSString stringWithFormat:@"%ld.jpg",(unsigned long)time] mimeType:@"image/jpeg"]; } } progress:^(NSProgress * _Nonnull uploadProgress) { if (progress) { progress(uploadProgress); } } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 取消设置状态栏风火轮 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; // API返回结果 NSInteger nCode = [[responseObject objectForKey:@"status"] integerValue]; NSString *strMsg = [responseObject objectForKey:@"msg"]; // id res = [responseObject objectForKey:@"data"]; if (sucess) { sucess(nCode,strMsg,responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { // 取消设置状态栏风火轮 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding]; NSLog(@"----%@",errResponse); if (faile) { faile([@"99999" intValue],@"网络请求失败\n请检查网络设置",nil); } }]; }
以上是关于相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传的主要内容,如果未能解决你的问题,请参考以下文章
即拿即用-选择头像,可以选择相册,拍照,查看大图,保存到本地