2011-10-19 32 views
0

我正在使用ASIHTTPRequest庫通過web服務發送多個圖像。我有一個2張圖片的數組,並且喜歡發送它們。但是隻有最新的圖像正在上傳。我認爲這是因爲圖像2正在覆蓋圖像1,因爲它們的名稱是相同的。上傳一組圖像會產生問題(ASIHTTPRequest)

如何更改我陣列中每個圖像的名稱,以便所有文件都能正確上傳?

這裏是我的代碼

NSString* filename2 = [NSString stringWithFormat:@"image.jpeg", i]; 
NSString *strURL = @"www.thisismyurl.com"; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
[request setDelegate:self]; 
[request setPostValue:@"This is sample text..." forKey:@"text"]; 

for (int i = 0; i < [array count]; i++) { 
    [request addData:[array objectAtIndex:i] withFileName:filename2 andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]]; 
} 
[request startAsynchronous]; 

編輯

這裏是我的代碼至今。首先,我從我的文檔目錄調用我的圖像 - >將其轉換爲NSData - >將NSData放入數組 - >將數組放入'addData:request'中。如何在發佈時爲每個圖片指定一個特定的名稱?

NSString *docDir2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString *pngFilePath2 = [NSString stringWithFormat:@"%@/foto2.jpeg",docDir2]; 
      UIImage *img2 = [[UIImage alloc] initWithContentsOfFile:pngFilePath2]; 
      NSData *imageData2 = UIImageJPEGRepresentation(img2,0.75); 

      NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/foto3.jpeg",docDir3]; 
      UIImage *img3 = [[UIImage alloc] initWithContentsOfFile:pngFilePath3]; 
      NSData *imageData3 = UIImageJPEGRepresentation(img3, 0.75); 

      [array addObject:imageData2]; 
      [array addObject:imageData3]; 

      [img2 release]; 
      [img3 release]; 

      if ([array count] > 0) { 

       NSString *strURL = @"this is my url"; 
       ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
       [request setDelegate:self]; 
       [request setPostValue:@"This is sample text..." forKey:@"text"]; 
       for (int i = 0; i < [array count]; i++) { 

        [request addData:[array objectAtIndex:i] withFileName:@"image.jpeg" andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]]; 

       } 
       [request startAsynchronous]; 


      } 
      else { 
       UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Message" message:@"You have to make some pictures" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
       [alertView show]; 
       [alertView release]; 
      } 
+0

數組內部究竟是什麼? – vikingosegundo

回答

2

肯定你的文件名是錯的。 [NSString stringWithFormat:@"image.jpeg", i];不是有效的格式。

NSString *strURL = @"www.thisismyurl.com"; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
[request setDelegate:self]; 
[request setPostValue:@"This is sample text..." forKey:@"text"]; 

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

    [request addData:[array objectAtIndex:i] 
     withFileName:[NSString strinWithFormat:@"image_%d.jpg", i+1] 
     andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]]; 

} 
[request startAsynchronous]; 
+0

感謝您的回覆,但它仍然無法正常工作(我替換了[請求addData:[array objectAtIndex:i] withFileName:filename2 andContentType:@「image/jpeg」forKey:[NSString stringWithFormat:@「image%d」,i + 1]]; - > [request addData:[array objectAtIndex:i] withFileName:@「image.jpeg」andContentType:@「image/jpeg」forKey:[NSString stringWithFormat:@「image%d」,i + 1]] ;任何線索? – BarryK88

+0

請在您的原始問題中提交代碼 – vikingosegundo

+0

因此,您再次提及相同的文件名稱 – vikingosegundo