AFN2.0上传进度条设置

在使用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上传时获取当前上传进度和上传内容大小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值