gpt4 book ai didi

iphone - 如何通过服务器上的数组上传图片?

转载 作者:行者123 更新时间:2023-11-28 20:40:39 25 4
gpt4 key购买 nike

在我的应用程序中,我必须使用 php 将多张图片上传到服务器。

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

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

以下是我的代码:

- (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);


}

}

请帮帮我……我试了很久了……

最佳答案

不过,您不应该使用数组来保存您的UIImages。我对此有严重的性能问题。你应该做的是保存一组图像路径,在你要上传图像的那一刻,只需使用路径来获取图像。在 iPhone 4 中有 30 多张图库中的图像时,我的应用程序开始崩溃并出现内存警告。

编辑(您问题的实际答案):

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

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

编辑2:

Dimple Panchal 是对的,您应该以异步方式进行。

关于iphone - 如何通过服务器上的数组上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8723825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com