2012-01-04 29 views
0

在我的應用程序中,我必須使用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

回答

3

剛一個雖然,你不應使用陣列來舉行你的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

1

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

-1

我認爲它是你的Web服務problem.please檢查你的服務,並使其能夠接收多個images.thanks你的代碼,我也面臨同樣的問題,現在我希望它的工作。

0

這是代碼的問題,因爲我們在最後一張圖片只存儲的循環中追加圖片,每次前一張圖片被下一張圖片數據覆蓋。我已經使用AFNetworking Library將多張圖片發送到服務器,它非常酷,並有所有的需要,使工作更好,沒有錯誤。

相關問題