将UIProgressView添加到NSURLConnection?
是否有可能添加一个UIProgressView到NSURLConnection? 是这样简单的获取数据的长度,然后将其设置为一个int然后将该int发送到我的其他类到我的UIProgressView?
有人可以告诉我如何做到这一点? 有没有任何例子或链接将我指向正确的方向?
谢谢!!!
在你的didReceiveResponse
函数中,你可以得到像这样的总文件大小 –
_totalFileSize = response.expectedContentLength;
。
在您的didReceiveData函数中,您可以添加最多一个字节收到计数器 –
_receivedDataBytes += [data length];
现在为了设置进度条到正确的大小,你可以简单地做 –
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize
(在didReceiveData
函数或代码中的其他地方)
不要忘记将保存字节数的variables添加到您的类中!
以下是如何实现代表以更新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]; }
我希望这有帮助..
接受的答案看起来太像这个。 https://stackoverflow.com/a/4255630这真是引起了我的注意。
(在didReceiveData函数或代码中的其他地方)
不要忘记将保存字节数的variables添加到您的类中!