在我的應用程序中,我必須使用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);
}
}
請幫我...我從一個很長一段時間試試這個..
嗨尼萊斯.....我有相同的問題,關於從iphone中的照片庫中選擇多個圖像,當用戶完成選擇圖像,然後按鈕上點擊所有選定的圖像應發送到server.As你已經實現了「多個圖像的選擇」,所以請給我提供一些博客,視頻,任何演示代碼或任何參考。謝謝很多:) – 2014-03-20 11:06:04