AFNetworking 2.0跟踪file upload进度

我对AFNetworking 2.0相对较新。 使用下面的代码片段,我已经能够成功地上传一张照片到我的url。 我想跟踪增量上传进度,但我找不到2.0版本的例子。 我的应用程序是iOS 7,所以我select了AFHTTPSessionManager。

任何人都可以提供一个如何修改这个片段来跟踪上传进度的例子吗?

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"myimage.jpg"], 1.0); [manager POST:@"http://myurl.com" parameters:dataToPost constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"attachment" fileName:@"myimage.jpg" mimeType:@"image/jpeg"]; } success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"Success %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Failure %@, %@", error, [task.response description]); }]; 

AFHTTPSession的接口不提供设置进度块的方法。 相反,你必须做到以下几点:

 // 1. Create `AFHTTPRequestSerializer` which will create your request. AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; // 2. Create an `NSMutableURLRequest`. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://www.myurl.com" parameters:dataToPost constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"attachment" fileName:@"myimage.jpg" mimeType:@"image/jpeg"]; }]; // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure %@", error.description); }]; // 4. Set the progress block of the operation. [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); }]; // 5. Begin! [operation start]; 

另外,您不必通过UIImage读取图像,然后使用JPEG重新压缩以获取NSData 。 只需使用+[NSData dataWithContentsOfFile:]直接从您的包中读取文件。

AFHTTPSessionManager的界面没有提供跟踪上传进度的方法。 但是 AFURLSessionManager

作为AFURLSessionManager的inheritance类AFHTTPSessionManager可以像这样跟踪上传进度:

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:kUploadImageURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:@"uploadFile" fileName:@"image" mimeType:@"image/jpeg"]; } error:nil]; NSProgress *progress; NSURLSessionDataTask *uploadTask = [[AFHTTPSessionManager sharedManager] uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (!error) { //handle upload success } else { //handle upload failure } }]; [uploadTask resume]; [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL]; 

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"fractionCompleted"] && [object isKindOfClass:[NSProgress class]]) { NSProgress *progress = (NSProgress *)object; //progress.fractionCompleted tells you the percent in CGFloat } } 

这里是方法2(更新)

使用KVO跟踪进度意味着在观察期间self需要活着。 更优雅的方式是AFURLSessionManager的方法setTaskDidSendBodyDataBlock ,就像这样:

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) { //during the progress }]; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:kUploadImageURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:@"uploadFile" fileName:@"image" mimeType:@"image/jpeg"]; } error:nil]; NSURLSessionDataTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (!error) { //handle upload success } else { //handle upload failure } }]; [uploadTask resume];