将UIProgressView添加到NSURLConnection吗? [英] Add UIProgressView to a NSURLConnection?
问题描述
是否可以将UIProgressView添加到NSURLConnection?是否像获取数据长度然后将其设置为int然后将该int发送到我的其他类到UIProgressView一样简单?
Is it possible to add a UIProgressView to a NSURLConnection? Is it as simple as getting the length of the data and then setting it to an int then sending that int to my other class to my UIProgressView?
有人可以告诉我该怎么做吗?是否有任何示例或链接可以为我指明正确的方向?
Can someone show me how to do this? Are there any examples or links that will point me in the right direction?
谢谢!
推荐答案
在您的didReceiveResponse
函数中,您可以像这样获得总文件大小-
In your didReceiveResponse
function you could get the total filesize like so -
_totalFileSize = response.expectedContentLength;
.
然后在didReceiveData函数中,您最多可以将接收到的总字节数累加到计数器中-
In your didReceiveData function you can then add up to a total bytes received counter -
_receivedDataBytes += [data length];
现在,为了将进度栏设置为正确的大小,您可以简单地进行操作-
Now in order to set the progressbar to the correct size you can simply do -
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize
(在didReceiveData
函数中或在代码中的其他地方)
(either in the didReceiveData
function or somewhere else in your code)
别忘了将包含字节数的变量添加到您的类中!
Don't forget to add the variables that hold the number of bytes to your class!
在这里,您可以实现委托以更新进度视图
Here's how you could implement the delegates in order to update progressview
-(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];
}
我希望这会有所帮助.
这篇关于将UIProgressView添加到NSURLConnection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!