如何通过服务器上的阵列上传图像?

问题描述:

在我的应用程序中,我必须使用php将多个图像上传到服务器。如何通过服务器上的阵列上传图像?

我正在制作一个NSMutableArray * arrImages,它包含从图库中选择的我的图像。

假设如果我选择了两个图像并尝试上传.....我只能上传最后选择的一个图像....我检查了我的循环...它工作正常......但我无法上传所有的两个图像...请帮助我。

下面是我的代码:

- (IBAction)btnTakePicture_Clicked:(id)sender 
{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Image Gallary", nil]; 
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
    actionSheet.alpha=0.90; 
    actionSheet.tag = 1; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 
    UIButton *btn = (UIButton *)sender; 
    intButton = btn.tag; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (actionSheet.tag) 
    { 
     case 1: 
      switch (buttonIndex) 
     { 
      case 0: 
      {    
#if TARGET_IPHONE_SIMULATOR 

       UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alert show]; 
       [alert release]; 

#elif TARGET_OS_IPHONE 

       UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
       picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
       picker.delegate = self; 
       //picker.allowsEditing = YES; 
       [self presentModalViewController:picker animated:YES]; 
       [picker release]; 

#endif 
      } 
       break; 
      case 1: 
      { 
       UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
       picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
       picker.delegate = self; 
       [self presentModalViewController:picker animated:YES]; 
       [picker release]; 
      } 
       break; 
     } 
      break; 

     default: 
      break; 
    } 
} 


-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 
    NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1); 
    UIImage *img = [[UIImage alloc] initWithData:dataImage]; 



    if (intButton == 0) 
    { 

     imgView1.image=img; 

    } 
    else if (intButton == 1) 
    { 
     imgView2.image=img; 
    } 
    else if (intButton == 2) 
    { 
     imgView3.image=img; 
    } 
    else if (intButton == 3) 
    { 
     imgView4.image=img; 

    } 
    else if (intButton == 4) 
    { 
     imgView5.image=img; 
    } 
    else if (intButton == 5) 
    { 
     imgView6.image=img; 
    } 
    else if (intButton ==6) 
    { 
     imgView7.image=img; 
    } 
    else if (intButton ==7) 
    { 
     imgView8.image=img; 
    } 

    [arrImages addObject:dataImage]; 
    //NSLog(@"%@",dataImage); 
    [picker dismissModalViewControllerAnimated:YES]; 
} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [self.navigationController dismissModalViewControllerAnimated:YES]; 
} 

这是我上传的代码

-(IBAction)upload:(id)sender 
{ 
    if ([arrImages count]>0) 
    { 

     NSString *urlString = @"http://your url.com/upload/uploader.php"; 

     // setting up the request object now 

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


     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


     NSMutableData *body = [NSMutableData data]; 





     for (int i = 0; i < [arrImages count]; i++) 
     { 

      //NSMutableData *body = [NSMutableData data]; 

      //[body appendData:[arrImages objectAtIndex:i] withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]]; 


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

      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"image%d.jpg\"\r\n",i + 1] dataUsingEncoding:NSUTF8StringEncoding]]; 

      [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

      //[body appendData:[NSData dataWithData:imageData]]; 
      [body appendData:[arrImages objectAtIndex:i]];   
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      // setting the body of the post to the reqeust 

      //[request setHTTPBody:body]; 
     } 
     [request setHTTPBody:body]; 

     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

     NSLog(@"%@",returnString); 


    } 

} 

请帮我...我从一个很长一段时间试试这个..

+0

嗨尼莱斯.....我有相同的问题,关于从iphone中的照片库中选择多个图像,当用户完成选择图像,然后按钮上点击所有选定的图像应发送到server.As你已经实现了“多个图像的选择”,所以请给我提供一些博客,视频,任何演示代码或任何参考。谢谢很多:) – 2014-03-20 11:06:04

刚一个虽然,你不应使用阵列来举行你的UIImages。我有严重的性能问题。你应该做的是在你要上传图像的那一刻保存一系列图像的路径,只需使用路径来获取图像。在具有超过30张图片的iPhone 4中,我的应用程序开始崩溃,并伴有内存警告。

Edit(对于你的问题实际的答案):

的问题是,发送新的上传请求之前,您应该等待第一个的答案。所以:

- >上传1 - >等待 - >上传完成 - >上传2 - >等待 - >上传完整...

编辑2:

酒窝潘卡尔是正确的,你应该以异步方式进行。

+0

我正在使用NSMutableArray数组......以保存uiimages ....和我必须jst上传8张图片....谢谢....... plz建议上面的代码.. – 2012-01-04 08:20:13

+0

这只是一个建议Nilesh。因为我花了很多时间重新做我的应用程序的体系结构。 – Peres 2012-01-04 08:23:31

+0

我真的非常感谢.........但是你可以帮助我解决这个问题......不管我是否以正确的方式接受了... plz先生 – 2012-01-04 08:26:23

sendSynchronousRequest由于您包含图像,会严重影响您的表现。 另外我不这么认为,所以您应该每次都追加边界。我不知道为什么这是失败的,但我认为你的数据结构发送不正确。我无法添加注解,因此下降的答案:(

NSString *boundary = [NSString stringWithString:@"------------------------14737829899999999999999999999990"]; 
    [theRequest addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"]; 
    [theRequest setHTTPMethod:@"POST"]; 
NSMutableData *body=[[NSMutableData alloc] init]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"xmldata\"\r\n\r\n %@\r\n\r\n",soapMessage] dataUsingEncoding:NSUTF8StringEncoding]]; 
[theRequest setHTTPBody:body]; 

凡SOAP消息将包含类似这样 循环

{ 
    str =[str stringByAppendingString:@"<sampleRoot>"]; 
    str = [str stringByAppendingFormat:@"<imageDate>%@</imageData>", imgData]; 
    str =[str stringByAppendingString:@"</sampleRoot>"]; 

} 
NSString *soapMessage=str; 

这仅仅是逻辑数据,您需要根据您的要求进行修改。

异步连接需要多一点的代码,但还是比较容易实现的,开始连接还需要一行代码:

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

然后,我们需要实现一些回调方法

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response 
{ 
    _data = [[NSMutableData alloc] init]; 
} 
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    [_data appendData:data]; 
} 
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{ 
    // Handle the error properly 
} 
-(void)connectionDidFinishLoading:(NSURLConnection*)connection 
{ 
    // Deal with the data 
} 

这些不会阻止UR执行如synchronousRequest

+0

你可以PLZ编辑上面的代码或PLZ提供一些建议.....谢谢你... – 2012-01-04 08:31:37

+0

我不能明白你想说什么.... 。你可以为我提供一些解决方案....或PLZ编辑上面的代码...感谢你。 – 2012-01-04 10:18:41

+0

酒窝可以üPLZ帮助发送同步请求.....谢谢你的很多 – 2012-01-05 05:04:27

我认为它是你的Web服务problem.please检查你的服务,并使其能够接收多个images.thanks你的代码,我也面临同样的问题,现在我希望它的工作。

这是代码的问题,因为我们在最后一张图片只存储的循环中追加图片,每次前一张图片被下一张图片数据覆盖。我已经使用AFNetworking Library将多张图片发送到服务器,它非常酷,并有所有的需要,使工作更好,没有错误。