いくつかの画像をサーバーにアップロードしたいので、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"];