Cococa Touch 中的 UIProgressbar

发布于 2024-08-13 08:46:03 字数 392 浏览 8 评论 0原文

我想根据从服务器下载的数据量显示 UIProgressbar。

我实现的一种方法是创建一个 NSURLConnection 并设置委托。 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 给了我预期的ContentLengh

并且在 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 我每次都会一部分一部分地获取数据,直到下载完所有数据。

但这并没有解决我的问题。

还有其他方法可以做到这一点吗?

感谢您的所有建议。

谢谢

I want to display an UIProgressbar depending on the amount of data downloaded from the server.

One way I implemented is I made a NSURLConnection and set the delegate.
The - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
gave me the expectedContentLengh

And in the - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
I am getting the data part by part every time until all the data are downloaded.

But this did not solve my problem.

Is there any other way to do this ?

All your suggestions are appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2024-08-20 08:46:03

如果您知道预期的内容长度,只需将迄今为止收到的内容除以您将获得的总数进行汇总即可:

// These should be properties of the delegate class
UIProgressView * progressView;
long long expected;
long long gotSoFar; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    gotSoFar += data.length;
    progressView.progress = gotSofar / expected;
}

如果您在除法上收到类型警告,请在进行除法之前将每个 long long 强制转换。

If you know the expected content length, just keep a running total of how much you've received so far divided by the total amount you'll get:

// These should be properties of the delegate class
UIProgressView * progressView;
long long expected;
long long gotSoFar; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    gotSoFar += data.length;
    progressView.progress = gotSofar / expected;
}

If you get type warnings on the division, cast each long long before doing the division.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文