1

是否可以将 UIProgressView 添加到 NSURLConnection?是否像获取数据的长度然后将其设置为 int 然后将该 int 发送到我的其他类到我的 UIProgressView 一样简单?

有人可以告诉我如何做到这一点吗?是否有任何示例或链接可以为我指明正确的方向?

谢谢!!!

4

2 回答 2

3

在您的didReceiveResponse函数中,您可以像这样获得总文件大小 -

_totalFileSize = response.expectedContentLength;.

然后,在您的 didReceiveData 函数中,您可以将接收到的总字节数计数器相加 -

_receivedDataBytes += [data length];

现在为了将进度条设置为正确的大小,您可以简单地执行 -

MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize

(在didReceiveData函数中或代码中的其他地方)

不要忘记将保存字节数的变量添加到您的类中!

以下是您如何实现委托以更新进度视图

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _totalFileSize = response.expectedContentLength;
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   _receivedDataBytes += [data length];
   MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
   [responseData appendData:data];
 }

我希望这有帮助..

于 2011-12-05T08:13:19.457 回答
0

接受的答案看起来太像这个了。https://stackoverflow.com/a/4255630 这才是真正引起我注意的地方。

(在 didReceiveData 函数或代码中的其他地方)

不要忘记将保存字节数的变量添加到您的类中!

于 2014-02-24T13:29:02.440 回答