下面是我的代码发送一个post request组成的4 base64Image数据到我的服务器。
NSString *postURL = [NSString stringWithFormat:@"name=%@&mobile=%@&email=%@&bday=%@&image=%@&img1=%@&img2=%@&img3=%@",@"abin",@123",@"abc@gmail.com",@"01-01-1990",[base64Value]image,[base64 value] of img1,[base64 value] of img2,[base64 value] of img3"];
NSData *postData = [postURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"MY_URL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
这里所有的图像值imag1,image,image都是base64值
向服务器发送此请求时,10次中有6次失败。
通过Wi-Fi成功率高,而通过移动数据成功率非常低。
使用下面的代码完成图像转换:
- (NSString *)encodeToBase64String:(UIImage *)image {
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSData *imageBase64Data = [data base64EncodedDataWithOptions:0];
NSString *imageBase64String = [[NSString alloc] initWithData:imageBase64Data encoding: NSUTF8StringEncoding];
return imageBase64String;
}
任何一次请帮助我一些想法,成功发送多个图像到服务器使用POST请求,即使网络连接缓慢。(2G)
>
dispatch_queue_t queue;
在viewdidload: if (!queue) {
queue = dispatch_queue_create("name", DISPATCH_QUEUE_CONCURRENT);
}
在你的方法中:
dispatch_async(queue, ^{
//do the post code here
});
dispatch_async(dispatch_get_main_queue(), ^{
//do the ui update part here
});
}