【问题标题】:Uploading an array of images gives problems (ASIHTTPRequest)上传图像数组会出现问题 (ASIHTTPRequest)
【发布时间】:2011-12-11 02:38:42
【问题描述】:

我正在使用 ASIHTTPRequest 库通过网络服务发送多个图像。我有一组 2 张图片,想发送它们。然而,只有最新的图像被上传。我认为这是因为图像 2 覆盖了图像 1,因为它们的名称相同。

如何更改数组中每个图像的名称,以便正确上传所有文件?

这是我的代码

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];
            }

【问题讨论】:

  • 数组里面到底是什么?

标签: iphone arrays image http upload


【解决方案1】:

你的文件名肯定是错误的。 [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];

【讨论】:

  • 感谢您的回复,但它仍然无法正常工作(我将 [request addData:[array objectAtIndex:i] 替换为 FileName: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]]; 有什么线索吗?
  • 所以你又引用了相同的文件名。
  • 我知道这一点。这就是我要问的:-)
  • 哎呀,你的回答完成了这项工作。非常感谢 vikingosegundo!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 2015-07-26
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多