下面是我的代码,用于将包含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];
此处所有图片值img1,图片,图片均为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)
答案 0 :(得分:0)
: -
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
});
}