NSURLSession用于通过多个URL发送多个图像

时间:2014-06-17 13:29:21

标签: ios objective-c ios7 nsurlconnection

以下代码用于发送多个图像和文本。但只有一个图像保存在Web服务器中。这里的问题是我需要从第一个网址获得响应,并且我已将其分配给第二个网址。

NSLog(@"PassedID%@",PassedUserId);

NSLog(@"integer=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"Person"]);


NSString *CategoryId=@"3";
NSString *imagename=@"ComparisonObject";

 NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,GenderText.text,imagename];
  NSLog(@"%@",requestString);

NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/InsertObjectImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *uploadImage1 = [session dataTaskWithRequest:request completionHandler:^(NSData *data2, NSURLResponse *response, NSError *error) {
    // Finish uploading image 1
    // Get response and data to prepare to update image 2

    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(@"recievedData%@",receivedData);
    NSString *imagename=@"ComparisonObject";

    NSString *requestString1 =[NSString stringWithFormat:@"UserId=%@&ImageId=%@&=ImageName%@",PassedUserId,compareId,imagename];
    NSLog(@"%@",requestString1);

    NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage?%@",requestString1];

    NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    NSURLSessionDataTask *uploadImage2 = [session dataTaskWithRequest:request2 completionHandler:^(NSData *data1, NSURLResponse *response, NSError *error) {


        NSLog(@"recievedData%@",receivedData);
        NSString *imagename=@"ComparisonObject";

        NSString *requestString1 =[NSString stringWithFormat:@"UserId=%@&ImageId=%@&=ImageName%@",PassedUserId,compareId,imagename];
        NSLog(@"%@",requestString1);

        NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage?%@",requestString1];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
        NSData *data = UIImageJPEGRepresentation(chosenImage2, 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];


        // Finish uploading image 2
    }];

}];

0 个答案:

没有答案