ios使用http来上传图片实现方法
if (parameters) {
int genderNumber = 1;
self.token = loginToken;
self.personPK = kidPK;
self.personName = personNameL;
self.personNickName = nickNameL;
self.gender = genderL;
self.birthday = birthdayL;
self.imageData = imageL;
self.imageName = imageNameL;
if ([self.gender isEqualToString:@"女"]) {
genderNumber = 2;
}
[parameters setValue:self.token forKey:@"token"];
[parameters setValue:self.personPK forKey:@"personPk"];
[parameters setValue:self.personName forKey:@"person.name"];
[parameters setValue:self.personNickName forKey:@"person.nickname"];
[parameters setValue:[NSString stringWithFormat:@"%d", genderNumber] forKey:@"person.gender"];
[parameters setValue:self.birthday forKey:@"birthday"];
[parameters setValue:self.personNickName forKey:@"turFileFileName"];
//[parameters setValue:image forKey:@"turFile"];
}
self.LoadStatus = LOADING;
self.strURL = tmp;
NSLog(@"the url is %@",self.strURL);
self.activeDownload = [NSMutableData data];
//分界线的标识符
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//根据url初始化request
NSMutableURLRequest* requestL = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.strURL]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
//得到图片的data
// self.imageData = UIImagePNGRepresentation(image);
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [parameters allKeys];
//遍历keys 注意这个除去了data类型只是包含了string类型的
for(int i=0;i<[keys count];i++)
{
//得到当前key
NSString *key=[keys objectAtIndex:i];
//如果key不是pic,说明value是字符类型,比如name:Boris
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[parameters objectForKey:key]];
}
//注意这里的turFile其实就是要上传的file的字段名字
////添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"turFile\"; filename=\"boris.jpg\"\r\n"];
//声明上传文件的格式
[body appendFormat:@"Content-Type: image/jpeg\r\n\r\n"]; //这里因为之前压缩生成的是jpg类型的图片所以需要表明jpeg,如果是png就是png
//声明结束符:--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:self.imageData];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[requestL setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[requestL setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[requestL setHTTPBody:myRequestData];
//http method
[requestL setHTTPMethod:@"POST"];
//建立连接,设置代理
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:requestL delegate:self];
self.loadConnection = conn;
[conn release];
if (self.loadConnection != nil)
{
[self.loadConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
压缩的方法实现:
-(void)compraseImage
{
UIImage *largeImage = self.pictureViewUpload.image;
double compressionRatio = 1;
int resizeAttempts = 5;
NSData * imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
//NSLog(@"Starting Size: %i", [imgData length]);
//Trying to push it below around about 0.4 meg
while ([imgData length] > 400000 && resizeAttempts > 0) {
resizeAttempts -= 1;
//NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
//NSLog(@"%i Attempts Remaining",resizeAttempts);
//Increase the compression amount
compressionRatio = compressionRatio*0.5;
//NSLog(@"compressionRatio %f",compressionRatio);
//Test size before compression
//NSLog(@"Current Size: %i",[imgData length]);
imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
//Test size after compression
//NSLog(@"New Size: %i",[imgData length]);
}
//Set image by comprssed version
//self.pictureView.image = [UIImage imageWithData:imgData];
//Check how big the image is now its been compressed and put into the UIImageView
// *** I made Change here, you were again storing it with Highest Resolution ***
NSData *endData = UIImageJPEGRepresentation(largeImage,compressionRatio);
//NSLog(@"Ending Size: %i", [endData length]);
/* NSString *path = [self createPath:@"myImage.jpg"];
NSLog(@"%@",path);
[endData writeToFile:path atomically:YES];*/
[self startUploadingRequestWithData:endData andImage:self.pictureViewUpload.image];
}
ios使用http来上传图片实现方法的更多相关文章
- iOS开发 GET、POST请求方法(NSURLSession篇)
NSURLConnection,在iOS9被宣布弃用,本文不使用NSURLConnection进行网络编程,有兴趣的童鞋可以参考: [iOS开发 GET.POST请求方法(NSURLConnectio ...
- iOS开发 GET、POST请求方法(NSURLConnection篇)
Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...
- IOS中图片拉伸技巧与方法总结(转载)
以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...
- iOS——浅谈iOS中三种生成随机数方法
ios 有如下三种随机数方法:
- iOS项目的完整重命名方法图文教程
原文链接:http://www.cocoachina.com/ios/20150104/10824.html iOS项目的完整重命名方法图文教程 前言:在iOS开发中,有时候想改一下项目的名字,都会遇 ...
- iOS开发中的Html解析方法
iOS开发中的Html解析方法 本文作者为大家介绍了在iOS开发中的Html解析方法,并同时提供了Demo代码的下载链接,Demo 解析了某个网站(具体可在代码中查看)的html网页,提取了图片以及标 ...
- iOS开发 GET、POST请求方法:NSURLSession篇
NSURLConnection,在iOS 9被宣布弃用,本文不使用NSURLConnection进行网络编程,有兴趣的童鞋可以参考: iOS开发 GET.POST请求方法(NSURLConnectio ...
- iOS拨打电话(三种方法)
iOS拨打电话(三种方法) 查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯 ...
- iOS微信实现第三方登录的方法
这篇文章主要介绍了iOS微信第三方登录实现的全过程,一步一步告诉大家iOS微信实现第三方登录的方法,感兴趣的小伙伴们可以参考一下 一.接入微信第三方登录准备工作.移动应用微信登录是基于OAuth2 ...
随机推荐
- BZOJ 4425: [Nwerc2015]Assigning Workstations分配工作站
难度在于读题 #include<cstdio> #include<algorithm> #include<queue> using namespace std; p ...
- Makefile基础(一)
在大型的C语言项目中,一般都是由多个源文件编译链接形成的可执行程序,而这些源文件的处理步骤,通常交给Makefile来管理,Makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后 ...
- Service 回顾
绑定本地service需要实现onBind()方法
- 写iOS SDK注意事项
转载http://www.devtang.com/blog/2015/01/31/write-sdk-tips/
- [python IO学习篇]补充打开中文路径的文件
http://blog.csdn.net/mottolinux/article/details/525600621 关于Python编码的基本常识 在python里面 “明文”是unicode类型和s ...
- 多元线性回归(pandas/scikit-learn)
import pandas as pd from sklearn.cross_validation import train_test_split from sklearn.linear_model ...
- 【bzoj2346】[Baltic 2011]Lamp 堆优化Dijkstra
题目描述 2255是一个傻X,他连自己家灯不亮了都不知道.某天TZ大神路过他家,发现了这一情况,于是TZ开始行侠仗义了.TZ发现是电路板的问题,他打开了电路板,发现线路根本没有连上!!于是他强大的脑力 ...
- 【Luogu】P3203弹飞绵羊(分块)
题目链接 正解是LCT但我不会呀蛤蛤蛤蛤蛤 (分块我也没想出来 把区间分成根n个块,每个块内记录两个东西,就是该位置弹多少次能够弹出这个块,以及该位置弹到最后弹出去了之后能够弹到哪里. 然后查询就一个 ...
- ubuntu 安装tomcat<服务器>
一.下载tomcat 可以先下载到本地,然后ftp到服务器 官方 Apache Tomcat 的下载页面(下面的链接是apache自己的镜像服务器的地址,不同网络连接的话,apache会给出不同的镜像 ...
- phpMyAdmin操作之改管理员密码
1.登录phpMyAdmin 默认地址:http://localhost/phpmyadmin 2.点击用户按钮 3.往下拉找到修改密码 点执行就修改了 注意: 如果再次登录时报错提示: #1045 ...