AFNetWorking 圖片上傳,進度條監控



今天項目,發現圖片上傳這個多次用到,記錄一下,以備后用

     

    MBProgressHUD *hud = [MBProgressHUD showProcessHUD:@"" process:0];
    
    NSDictionary *dic = @{@"type" : @"img", @"uploadLableName" : @"pic"};
    NSString *postUrl = [NSString stringWithFormat:@"upload.url"];
    AFHTTPRequestOperationManager* _manager = [AFHTTPRequestOperationManager manager];
    
    NSMutableURLRequest* request = [_manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:postUrl parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:fileData name:@"pic" fileName:@".jpg" mimeType:@"image/png"];
    } error:nil];
    
    AFHTTPRequestOperation *operation = [_manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (200 == [responseObject[@"status"] integerValue]) {
            success(responseObject);
        }else
        {
            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"message"]]];
        }
        [hud hide:YES];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [hud hide:YES];
        [MBProgressHUD showMessage:@"上傳失敗!"];
    }];
    
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
         hud.progress = (float)totalBytesWritten/totalBytesExpectedToWrite;
    }];
    
    [operation start];




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com