gpt4 book ai didi

ios - AFMultipartFormData 上传完成后运行方法

转载 作者:行者123 更新时间:2023-11-29 00:36:22 26 4
gpt4 key购买 nike

我使用 AFMultipartFormData 将多张图片发送到服务器,使用 for 循环一次获取一张图片:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;
NSString *mystring = @"noName";
//FOR Loop Start
for(NSData *eachImage in dataStringArray) {

NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFormData:eachImage name:@"myImage"];
[formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
name:@"FileName"];
} error:&error];

NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
return;
}

[uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];

}];
[task resume];
}//for end
[self makeJSON:uploadedImageIDs];

但不知何故,它之前调用了 makeJSON 方法。由于有多张图片,所以我需要在所有图片上传完成后调用它。

最佳答案

网络处理将以异步方式运行,因此您需要在网络 completionHandler 中计数以了解所有任务何时完成。像这样

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;
NSString *mystring = @"noName";
NSInteger count = 0;

//FOR Loop Start
for(NSData *eachImage in dataStringArray) {

NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFormData:eachImage name:@"myImage"];
[formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
name:@"FileName"];
} error:&error];

NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
count++;

if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
[uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
}

if (count == dataStringArray.count) {
[self makeJSON:uploadedImageIDs];
}
}];
[task resume];
}//for end

关于ios - AFMultipartFormData 上传完成后运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40507074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com