【发布时间】: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