我正在使用ASIHTTPRequest库通过Web服务发送多个图像。我有一个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];
修改
这是我到目前为止的代码。首先,我从我的文档目录中调用我的图像 - &gt;将其转换为NSData - &gt;将NSData放入数组中 - &gt;将数组放在'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 :(得分: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];