gpt4 book ai didi

ios - UIProgressView 不显示

转载 作者:行者123 更新时间:2023-11-29 03:40:57 25 4
gpt4 key购买 nike

我有一个从服务器下载图片的应用程序。我想向用户展示进度。我已经在苹果文档上阅读了有关 UIProgressView 的内容,并在该网站上阅读了许多答案,但我无法让它工作。这是我的代码在我的 viewDidLoad 中,我有

_profileProgressView.hidden= YES;
_profileProgressView.progress = 0.0;

在我的 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 中,我显示 UIProgressView,然后获取图像的预期大小。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.profileProgressView.hidden= NO;
[self.remote_response setLength:0];
received = 0;
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
NSLog(@"Content Length::%@", self.filesize);
}

在我的 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d 中,我计算下载图像的百分比,然后更新 UIProgressView 的进度并记录下载进度。

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.remote_response appendData:d];

NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.remote_response length]];
dispatch_async( dispatch_get_main_queue(), ^ {
float progress = (_profileProgressView.progress + ([self.filesize floatValue]/[resourceLength floatValue])*100);
[self.profileProgressView setProgress:progress animated:YES];
NSLog(@"Downloading %.0f%% complete", _profileProgressView.progress);
});
}

当我运行这段代码时

  1. UIProgressView 永远不会显示

  2. 我得到的日志是下载完成 0%。我期望获得正在下载的图像的日志。

此外,我认为我应该在 viewDidDownloadallocinit UIProgressView,当我这样做时,我得到了 UIProgressView 但它没有显示进度,图像下载完成后不会隐藏。

最佳答案

我认为您使用 NSNumber 对数据类型进行装箱和拆箱使数学变得过于复杂。

我要做的是创建三个变量:

double fileLength;
double lastProgress;
double currentLength;

然后做简单的数学计算:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
fileLength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
double length = [d length];
currentLength += length;
double progress = currentLength/fileLength;

if (lastProgress < progress) {
profileProgressView.progress = progress;
lastProgress = progress;
}
}

关于ios - UIProgressView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18456689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com