代码让我很高兴将图像发布到Web服务器中。它的工作智能为单一图像。我用来发布单个图像的代码是
NSLog(@"%@",requestString);
NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertFacialImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
然后我没能跳过障碍,无法发布2张不同服务的图片。我失败的真相是上传第1张图片后,服务器生成响应然后我将响应附加到第二服务。我做得很好,但失败了,因为我连接运行2次2张图片。但是Web服务团队要求我在一个连接中运行它。我使用的代码,我需要修改的是
发布第一张图片
NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq) {
NSLog(@"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(@"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Status code: %ld", (long)[response statusCode]);
生成响应
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError *e;
jsonDict1 = [NSJSONSerialization JSONObjectWithData:receivedData options: NSJSONReadingMutableContainers error: &e];
compareId = [jsonDict1 valueForKey:@"ImageId"];
NSLog(@“JSONN%@”,jsonDict1);
compareId = [results.firstObject objectForKey:@"ImageId"];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.compareId forKey:@"Sendy"];
[defaults synchronize];
NSLog(@"compareId:%@",compareId);
results = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&e];
}
}
在此步骤之后,响应生成良好,我可以将其传递给第二个字符串。所以服务器响应没问题。
*发布第二张图片*
NSString *url1=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/UpdateObjectImage?%@",requestString1];
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init] ;
[request1 setURL:[NSURL URLWithString:url1]];
[request1 setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request1 setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage2, 0.2f);
[request1 addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[NSData dataWithData:data]];
[request1 setHTTPBody:body1];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request1 delegate:self];
if (connReq) {
NSLog(@"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(@"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSLog(@"Status code: %ld", (long)[response statusCode]);