我使用AFNetworking3.0在iOS上传一些图像,代码如下所示, images 是UIImage的数组。 我有两个问题:
在进度块中,当uploadProgress.fractionCompleted
等于1.0时打印日志A,在completionHandler
块的头部打印日志B,然后我找到了日志A的时间和B差别很大。有什么不对或正常吗?
当我上传多张图片时,为了获得更好的性能和更少的上传时间,我应该创建多个请求或者如下所示,这是正确的选择吗?
代码如下:
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URL_IMAGE_SERVER parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for(UIImage *eachImage in images) {
NSData *imageData = UIImageJPEGRepresentation(eachImage, 0.5);
[formData appendPartWithFileData:imageData name:@"upload_picture" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
}
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
} else {
}
}];
[uploadTask resume];