iOS post上传图片
来源:互联网 发布:机器码加密软件破解 编辑:程序博客网 时间:2024/05/21 14:53
在项目开发中,经常会碰到图片上传,有些后台比较坑,上传图片一定需要按照服务端那边格式把body post过去才行,格式一定要正确,你懂的,有些在客户端直接传流即可。下面说的比较坑的图片怎么post,源代码网上也比较多,直接分享下代码。
上面有注释的很清晰就不一一解释了。
1._mResponseData 是NSMutableData对象。
2.解释下ImageName,这是个key,就是你要上传给服务器的入参,看服务器怎么给你定义的,我这边是ImageName。
3.parameters这个字典封装后台给你定义的入参,一般都以json格式,我这边parameters里面的入参有ImageName。
4.请求也粘贴在下面,可直接复制代码,返回的数组需要自己改动下。
#pragma mark - 上传图片
- (void)requestPhotoApi:(NSDictionary *)parameters;
{
NSString *urlAsString = [NSStringstringWithFormat:@"%@?key=%@&",[kTestRequestURLstringByAppendingString:@"sys/webapi/common/upload.do"],[FAUserModelgetUserToken]];
//分界线的标识符
NSString *TWITTERFON_FORM_BOUNDARY =@"AaB03x";
//根据url初始化request
NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:urlAsString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20];
//分界线 --AaB03x
NSString *MPboundary=[[NSStringalloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSStringalloc]initWithFormat:@"%@--",MPboundary];
////要上传的图片
UIImage *image=[parametersobjectForKey:@"imageName"];
//得到图片的data
NSData *data =UIImagePNGRepresentation(image);
//http body的字符串
NSMutableString *body=[[NSMutableStringalloc]init];
//参数的集合的所有key的集合
NSArray *keys= [parametersallKeys];
//遍历keys
for(int i=0;i<[keyscount];i++){
//得到当前key
NSString *key=[keysobjectAtIndex:i];
//如果key不是pic,说明value是字符类型,比如name:Boris
if(![keyisEqualToString:@"imageName"]) {
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[parametersobjectForKey: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=[[NSStringalloc]initWithFormat:@"\r\n%@",endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableDatadata];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[bodydataUsingEncoding:NSUTF8StringEncoding]];
//将image的data加入
[myRequestData appendData:data];
//加入结束符--AaB03x--
[myRequestData appendData:[enddataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSStringalloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:contentforHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSStringstringWithFormat:@"%d", (int)[myRequestDatalength]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
//建立连接,设置代理
NSURLConnection *conn = [[NSURLConnectionalloc] initWithRequest:requestdelegate:self];
//设置接受response的data
if (conn) {
_mResponseData = [[NSMutableDataalloc] init];
}
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_mResponseDatasetLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_mResponseDataappendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:_mResponseDataoptions:kNilOptionserror:nil];
NSLog(@"%@", dic);
if (self.buttonTag ==ButtonTag) {
DLog(@"11111:%@",dic[@"data"])
self.uploadSevenArray = [NSMutableArrayarrayWithArray:dic[@"data"]];
}
else {
DLog(@"22222:%@",dic[@"data"])
self.uploadEgihtArray = [NSMutableArrayarrayWithArray:dic[@"data"]];
}
[selfrefreshSendImImage];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
}
- iOS post上传图片
- ios 使用post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- ios 用http post 上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- iOS通过http post上传图片
- win64 安装sql server 2008R2时报错“检查到visual studio2008 版本”的解决办法
- 稳压电路之5V到3.3V的几种电源方案
- Java 设计模式(八) Proxy(代理)模式及Spring引申
- Path Sum 路径和(注:同时包含得到各个路径的模板:两种不同表达形式的代码)
- 使用脚本和定时任务自动备份管理服务器
- iOS post上传图片
- C++第五次实验
- 第十周-阅读程序
- qemu raw 文件的复制、迁移
- Linux系统编程-文件IO函数
- 多态问题
- 消息队列-metaq
- 17款最佳的代码审查工具
- iOS 9 storyBoard