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];
}