如何通过IOS中的AFNetworking在后台上传200多张图片

我试图上传超过200张图片(根据我的需要)当在后台应用程序。 我正在使用NSUrlSession(上传任务)。 它正在上传约。 20个图像很容易,但是在这个过程之后没有响应。 我正在使用一个单一的请求(不使用数组上传图像在服务器上的客户端要求)。 请给我任何想法与一些示例代码,因为我已经试过约。 10-15种不同的方法。

感谢提前

应用程序在特定时间后进入暂停状态(在后台)。 所有正在进行的任务仍然中止。

可能的方法是,应用程序可以使用beginBackgroundTaskWithExpirationHandler请求OS一些额外的时间来完成未完成的任务。

示例代码如下:

 __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]); [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier]; backgroundTaskIdentifier = backgroundTaskIdentifier; }]; 

注意:对于提及的200个图像,应用程序处于活动状态将是一项艰巨的任务。 尝试不同的方法,例如支持应用程序的背景模式。

尝试这个

我正在使用AFNetworking来做到这一点

第1步:创build您的请求

 NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD] parameters:param constructingBodyWithBlock:^(id formData) { [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] name:@"sendimage" fileName:[strImagePath lastPathComponent] mimeType:@"image/png" error:nil]; } error:nil]; 

第2步:在这里,我在给定的位置创buildstream

 [[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] completionHandler:^(NSError *error){ 

第3步:在这里我正在创build上传任务。

  ///here is file NSProgress *progress = nil; NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1 fromFile:[NSURL fileURLWithPath:strImagePath] progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error); }]; [uploadTask resume]; } }]; 

}

重复此代码上传与后台任务