如何将多个图像发送到服务器

时间:2014-06-28 04:35:18

标签: ios iphone objective-c ios7

我可以将单个图像发送到服务器。现在我需要修改代码以在two images中发送two different urls。我用来发送单个图像的代码是

NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",requestString];
NSLog(@"url1%@",url);
 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];

帮助我,先谢谢大家。

2 个答案:

答案 0 :(得分:1)

尝试以下代码确保正确分配图像和网址

UIImage * image1 ;
UIImage * image2;

NSString * imageUrl1;
NSString * imageUrl2;



NSMutableArray * arrImageData=[[NSMutableArray alloc]initWithObjects:image1,image2,nil];

NSMutableArray * arrImageUrls=[[NSMutableArray alloc]initWithObjects:imageUrl1,imageUrl2,nil];

for(int i=0; i < arrImageData.count ; i++){


    NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",[arrImageUrls objectAtIndex:i]];
    NSLog(@"url1%@",url);
    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"];

    UIImage * chosenImage1=[arrImageData objectAtIndex:i];

    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];

}

答案 1 :(得分:0)

为了一次向同一个url发送多个图像到服务器,您必须使用base64转换图像,然后将所有转换后的图像的字符串添加到JSONJSON格式化后,您只需将这些图像发送到服务器即可。在服务器端,您必须将这些base64转换后的图像的字符串解码为图像data并将其保存到目录中。对于base64 EncodeDecode,请参阅此link

要将两张图片发送到两个不同的网址,您可以继续Harish Kanojiya回答。

相关问题