我正在尝试实施一个进度条,用于将图像从服务器下载到我的应用程序。这就是我的相关代码:
NSString *queryStringss = [NSString stringWithFormat:@"%@", uploadURL];
queryStringss = [queryStringss stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
_manager = [AFHTTPRequestOperationManager manager];
_manager.responseSerializer=[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
NSString *usernameEncoded = marker.title;
NSDictionary *params = @{@"username": usernameEncoded, @"count": [object valueForKey:@"count"]};
progBar.hidden = NO;
[_manager POST:queryStringss parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float prog = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
dispatch_async(dispatch_get_main_queue(), ^(void){
[progBar setProgress:prog];
});
NSLog(@"%f%% Uploaded", (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100));
}];
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:responseObject[@"image"] options:0];
image.image = [UIImage imageWithData:decodedData scale:300/2448];
dispatch_async(dispatch_get_main_queue(), ^(void){
[image setHidden:NO];
[respondButton setHidden:NO];
[xButton setHidden:NO];
});
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
此外,在viewDidLoad
中,我初始化进度条(在@implementation
块中声明:
progBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[progBar setFrame:CGRectMake(10, 264, 300, 10)];
[self.view addSubview:progBar];
progBar.hidden = YES;
我的代码有效 - 我在任何时候都看不到显示进度条。
答案 0 :(得分:0)
您似乎尚未将进度条添加到视图中。我们假设您在视图控制器的viewDidLoad
中执行此操作:
- (void)viewDidLoad
{
// progBar needs to be a @property
_progBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[_progBar setFrame:CGRectMake(0, 10, 300, 10)];
[self.view addSubview:_progBar];
_progBar.hidden = YES;
}
- (void)doYourHTTPCall
{
_progBar.hidden = NO;
NSString *queryStringss = [NSString stringWithFormat:@"%@", uploadURL];
queryStringss = [queryStringss stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
_manager = [AFHTTPRequestOperationManager manager];
_manager.responseSerializer=[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
NSString *usernameEncoded = marker.title;
NSDictionary *params = @{@"username": usernameEncoded, @"count": [object valueForKey:@"count"]};
[_manager POST:queryStringss parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float prog = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
dispatch_async(dispatch_get_main_queue(), ^(void){
[progBar setProgress:prog];
});
NSLog(@"%f%% Uploaded", (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100));
}];
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:responseObject[@"image"] options:0];
image.image = [UIImage imageWithData:decodedData scale:300/2448];
dispatch_async(dispatch_get_main_queue(), ^(void){
[image setHidden:NO];
[respondButton setHidden:NO];
[xButton setHidden:NO];
});
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
}
答案 1 :(得分:0)
我必须像这样重构我的代码 - 唯一奇怪的是var BASE_URL = "https://stackoverflow.com/";
数字不是0.0 - 1.0(因为字节我假设)。它实际上是一个负数,从-1,000开始到-110,000(或类似的东西)所以你可以看到,我不得不将这个数乘以一个非常小的负分数来移动小数点以便它介于0.0和1.0之间:
percentDone