一、点击头像图片 或者按钮 在相册选择照片返回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 = .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 > ) {
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([@"" intValue],@"网络请求失败\n请检查网络设置",nil);
}
}];
}

相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传的更多相关文章

  1. 文件的上传(表单上传和ajax文件异步上传)

    项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举的主要对于小文件上传的处理! 资源下载: 一. ...

  2. 普通文件的上传(表单上传和ajax文件异步上传)

    一.表单上传: html客户端部分: <form action="upload.ashx" method="post" enctype="mul ...

  3. 文件的上传(1)(表单上传和ajax文件异步上传)

    文件的上传(表单上传和ajax文件异步上传) 项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举 ...

  4. PHP流式上传和表单上传(美图秀秀)

    最近需要开发一个头像上传的功能,找了很多都需要授权的,后来找到了美图秀秀,功能非常好用. <?php /** * Note:for octet-stream upload * 这个是流式上传PH ...

  5. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  6. [转]html5表单上传控件Files API

    表单上传控件:<input type="file" />(IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允许选择多个文件:< ...

  7. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  8. Linux 基础命令-CURL 表单上传文件

    CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...

  9. 一般处理程序上传文件(html表单上传、aspx页面上传)

    html 表单上传文件        一般处理程序由于没有 apsx 页面的整个模型和控件的创建周期,而比较有效率.这里写一个用 html 表单进行文件上传的示例.        1. 表单元素选用 ...

随机推荐

  1. MotionEvent分析及ImageView缩放实现

    这个类在各种View和用户的手势操作之间的交互存在很大的自定义空间.要理解清楚这个类的一些特性和意义,对自定义的新型控件很有帮助 先翻译一下开发者文档的描述 Overview Motion event ...

  2. jquery特效(3)—轮播图①(手动点击轮播)

    写了一个轮播图练练手,先写了一个手动点击轮播的轮播图,随后我会慢慢接着深入写自动轮播图和鼠标悬浮图片停止移动轮播图等,虽然今天我生日,但是代码还是得写的,不能找借口放松自己,原地踏步也算后退. 下面来 ...

  3. 浏览器中的BOM和DOM

    BOM 浏览器对象模型 提供了独立于内容而与浏览器窗口进行交互的对象.描述了与浏览器进行交互的方法和接口,可以对浏览器窗口进行访问和操作,譬如可以弹出新的窗口,改变状态栏中的文本,对Cookie的支持 ...

  4. java后台获取cookie里面值得方法

    String admissionNo = ""; //得到所有的cookies Cookie[] cookies = this.getRequest().getCookies(); ...

  5. 使用boost库生成 随机数 随机字符串

    #include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...

  6. POJ 2970 The lazy programmer(贪心+单调优先队列)

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  7. bzoj 2169 连边——去重的思想

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2169 如果之前都去好重了,可以看作这次连的边只会和上一次连的边重复. 可以认为从上上次的状态 ...

  8. [转]解决pycharm无法导入本地包的问题(Unresolved reference 'tutorial')

    原文地址:https://www.cnblogs.com/yrqiang/archive/2016/03/20/5297519.html

  9. jq之鼠标事件

    以防自己忘记,最重要的是hover效果的 鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的.   (1):click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发 ...

  10. Alien Flowers

    题意: 求含有A个"RR",B个"RB",C个"BB",D个"BR"的字符串个数. 解法: 首先考虑"BR&q ...