AFN2.0上传进度条设置

来源:互联网 发布:淘宝店服装细节模板 编辑:程序博客网 时间:2024/05/21 10:28

在使用AFN上传图片的时候如果是3.0版本可以直接获取到当前上传的进度。

self.progressView1.progress = 1.0 * uploadProgress.completedUnitCount/ uploadProgress.totalUnitCount;

如果使用2.0只能看到成功或者失败尝试好的方法进行操作

// 1. Create `AFHTTPRequestSerializer` which will create your request.    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];    // 2. Create an `NSMutableURLRequest`.    NSMutableURLRequest *request =    [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://116.255.251.220:680/api/Files/PostFile"                                    parameters:nil                     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) {                                         [self.progressView1 hidePopUpViewAnimated:YES];                                         NSLog(@"Success %@", responseObject);                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                                         [self.progressView1 hidePopUpViewAnimated:YES];                                         NSLog(@"Failure %@", error.description);                                     }];    // 4. Set the progress block of the operation.    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,                                        long long totalBytesWritten,                                        long long totalBytesExpectedToWrite) {        [self.progressView1 showPopUpViewAnimated:YES];        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);        dispatch_async(dispatch_get_main_queue(), ^{            self.progressView1.progress = 1.0 * totalBytesWritten/ totalBytesExpectedToWrite;        });    }];    // 5. Begin!    [operation start];

上述就是使用AFN2.0上传时获取当前上传进度和上传内容大小。

0 0
原创粉丝点击