我正在使用 POST 请求将一些数据上传到服务器,并且我正在尝试根据以下内容更新 UIProgressView 的进度totalBytesWritten
的财产didSendBodyData
的方法NSURLConnection
。使用下面的代码,我无法正确更新进度视图,在完成之前它始终为 0.000。我不确定要乘以或除以什么才能获得更好的上传进度。
我将不胜感激提供的任何帮助!代码:
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
NSNumber *progress = [NSNumber numberWithFloat:(totalBytesWritten / totalBytesExpectedToWrite)];
NSLog(@"Proggy: %f",progress.floatValue);
self.uploadProgressView.progress = progress.floatValue;
}
您必须将 bytesWritten 和 bytesExpected 转换为float
要划分的值。
float myProgress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
progressView.progress = myProgress;
否则,除 2 个整数后,您将得到 0 或其他数字。
ie: 10 / 25 = 0
10.0 / 25.0 = 0.40
Objective-C 提供了modulus
操作员%
用于确定余数,对于整数除法很有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)