gpt4 book ai didi

ios - 如何在服务器上发布多张图片?

转载 作者:行者123 更新时间:2023-11-28 21:03:08 27 4
gpt4 key购买 nike

我正在尝试将多张图片发送到服务器,但只有一张图片发送到服务器。我正在尝试保存多张图片。我的代码在下面,请告诉我哪里错了。我看过这个发布多张图片的例子。

Upload multiple images in one request

谢谢

NSData *imageData1 = UIImageJPEGRepresentation(photo1, 0.8);
NSData *imageData2 = UIImageJPEGRepresentation(photo2, 0.8);
NSData *imageData3 = UIImageJPEGRepresentation(photo3, 0.8);
NSData *imageData4 = UIImageJPEGRepresentation(image1, 0.8);
NSData *imageData5 = UIImageJPEGRepresentation(image2, 0.8);

NSString *returnString;
NSDictionary *aParametersDic= @{@"input_100":userIDStr,@"input_1":regstrationNumberStr,@"input_88":regsDateStr,@"input_2":nameofHealthSeekerString,@"input_83":ageString1,@"input_84":weightStr,@"input_85":heightStr,@"input_3":motherNameStr,@"input_4":addressStr,@"input_5":phonenumberStr,@"input_95":alternativeNumberStr,@"input_6":emailStr,@"input_7":whatsAppStr,@"input_96":idProofStr1,@"input_8":professionStr1,@"input_9":maritialStatusStr,@"input_11":presentHistoryStr,@"input_12":pastHistoryStr,@"input_13":familyHistoryStr,@"input_15":sleepStr,@"input_16":freshStatusStr,@"input_17":sleepPillsStr,@"input_20":DietStr,@"input_21":spiceStr,@"input_23":apetiteStr,@"input_25":foodChewingHabbit,@"input_27":breakfastItemStr,@"input_29":lunchItemStr,@"input_31":eveningItemStr,@"input_33":dinnerItemStr,@"input_94":mentalStatus,@"input_36":relationStr,@"input_38":constipationStr,@"input_39":urinaryStr,@"input_41":habitStr,@"input_45":tasteOfMouthStr,@"input_47":sexualStr,@"input_49":fatiqueStr,@"input_51":regularExerciseStr,@"input_53":workingHoursStr,@"input_55":favDishStr,@"input_57":otherDetailsStr,@"input_59":femalStatsStr,@"input_60":progressReportStr,@"input_77":referredByStr,@"input_75_1":hearAboutStr,@"input_78":willingStr,@"input_87":_mentorNameTextField.text,@"input_90":_phonenumberTextField.text,@"input_91":_addressTextField.text};
// It's contains other parameters.
NSDictionary *aImageDic = @{@"input_93":imageData1,@"input_98":imageData2,@"input_92":imageData3,@"input_82":imageData4,@"input_97":imageData5 }; // It's contains multiple image data as value and a image name as key
NSString *urlString = [NSString stringWithFormat:@"http://nse/apis/custom/?api=1&page=user_patient_form"]; // an url where the request to be posted
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url] ;

[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831e45466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
NSString *postData = [self getHTTPBodyParamsFromDictionary:aParametersDic boundary:boundary];
[postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding]];

[aImageDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if(obj != nil)
{
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"PostedImage\"; filetype=\"image/png\"; filename=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:obj]];
}
}];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if([[jsonDict objectForKey:@"status"]boolValue])
{
NSLog(@"data saved successfully");
}
else
{
NSLog(@"Error in saving the data");
}
});

当 PHP 响应为
returnString 的打印说明:

Array
(
[PostedImage] => Array
(
[name] => input_82
[type] => image/jpeg
[tmp_name] => /tmp/phpIfld44
[error] => 0
[size] => 225219
)
)

最佳答案

试试这个。

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:@"IOS Testing" forKey:@"subject"];
[parameters setValue:@"Message" forKey:@"message"];
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

NSMutableArray *multipleArray=[[NSMutableArray alloc] init];
[multipleArray addObject:[UIImage imageNamed:@"images.jpeg"]];
[multipleArray addObject:[UIImage imageNamed:@"pic3.png"]];
for (int i=0; i<multipleArray.count;i++ )
{
NSData *imageData;
imageData = UIImageJPEGRepresentation([multipleArray objectAtIndex:i], 1.0);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"uploaded_file[]"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:BASEURL]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse* httpResponse =
(NSHTTPURLResponse*)response;
if ([httpResponse statusCode] == 200) {
NSLog(@"success");
}

}];
}

关于ios - 如何在服务器上发布多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com