IOS图片上传功能实现总结
IOS图片上传主要分两种方式实现,一个是将图片信息以表单的形式上传,一种是将图片以JSON的格式上传。
首先要讲的是以这两个方式上传的一个比较明显的区别就是HTTP Header是如何标记的,以表单的形式上传时HTTP Header应该设置为multipart/form-data,以JSON格式上传时HTTP Header应该设置为application/json。
- Content-Type: multipart/form-data; boundary=${bound}
- Content-Type: application/json
了解了这些那我们在IOS项目中应该如何运用他们呢?
一、表单上传图片
- //分界线的标识符
- NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
- //根据url初始化request
- NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
- cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
- timeoutInterval:10];
- //分界线 --AaB03x
- NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
- //结束符 AaB03x--
- NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
- //要上传的图片
- UIImage *image=[params objectForKey:@"pic"];
- //得到图片的data
- NSData* data = UIImagePNGRepresentation(image);
- //http body的字符串
- NSMutableString *body=[[NSMutableString alloc]init];
- //参数的集合的所有key的集合
- NSArray *keys= [params allKeys];
- //遍历keys
- for(int i=0;i<[keys count];i++)
- {
- //得到当前key
- NSString *key=[keys objectAtIndex:i];
- //如果key不是pic,说明value是字符类型,比如name:Boris
- if(![key isEqualToString:@"pic"])
- {
- //添加分界线,换行
- [body appendFormat:@"%@\r\n",MPboundary];
- //添加字段名称,换2行
- [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
- //添加字段的值
- [body appendFormat:@"%@\r\n",[params objectForKey:key]];
- }
- }
- ////添加分界线,换行
- [body appendFormat:@"%@\r\n",MPboundary];
- //声明pic字段,文件名为boris.png
- [body appendFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"boris.png\"\r\n"];
- //声明上传文件的格式
- [body appendFormat:@"Content-Type: image/png\r\n\r\n"];
- //声明结束符:--AaB03x--
- NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
- //声明myRequestData,用来放入http body
- NSMutableData *myRequestData=[NSMutableData data];
- //将body字符串转化为UTF8格式的二进制
- [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
- //将image的data加入
- [myRequestData appendData:data];
- //加入结束符--AaB03x--
- [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
- //设置HTTPHeader中Content-Type的值
- NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
- //设置HTTPHeader
- [request setValue:content forHTTPHeaderField:@"Content-Type"];
- //设置Content-Length
- [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
- //设置http body
- [request setHTTPBody:myRequestData];
- //http method
- [request setHTTPMethod:@"POST"];
- //建立连接,设置代理
- NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- //设置接受response的data
- if (conn) {
- mResponseData = [[NSMutableData data] retain];
- }
二、JSON上传图片
- //根据url初始化request
- NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
- cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
- timeoutInterval:10];
- //要上传的图片
- UIImage *image=[params objectForKey:@"pic"];
- //得到图片的data
- NSData* data = UIImagePNGRepresentation(image);
- //base64编码
- NSString *photoStr = [data base64Encoding];
- //参数字典
- NSDictionary *params = [[NSDictionary alloc] init];
- [params setObject:@{@"avatar": photoStr} forKey:@"profile"];
- NSData *myRequestData = [NSMutableData dataWithData:[NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil]];
- //设置http header
- [request.request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
- [request.request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
- //设置http body
- [request setHTTPBody:myRequestData];
- //http method
- [request setHTTPMethod:@"POST"];
- //建立连接,设置代理
- NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- //设置接受response的data
- if (conn) {
- mResponseData = [[NSMutableData data] retain];
- }