我想将一些图像上传到服务器,所以我使用 AFNetWork 发布文件。代码在这里:
UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];
__block int i = 0;
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
for(UIImage *eachImage in array)
{
NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"abc%d.jpg",i ] mimeType:@"image/jpeg"];
i++;
}
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){.....}
这不行!但是当我使用下面的代码替换 multipartFormRequestWithMethod 方法中的“for”语句时,一切正常!但是如果我无法确认图像数组的计数,我必须使用“for”语句,我的代码有问题吗?
[formData appendPartWithFileData:UIImageJPEGRepresentation([array objectAtIndex:0], 0.5) name:@"image1" fileName:@"image1.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:UIImageJPEGRepresentation([array objectAtIndex:1], 0.5) name:@"image2" fileName:@"image2.jpg" mimeType:@"image/jpeg"];