我正在使用 POST 请求将一些数据上传到服务器,我正在尝试根据 didSendBodyData
的 totalBytesWritten
属性更新 UIProgressView 的进度NSURLConnection
的方法。使用下面的代码,我没有正确更新进度 View ,在完成之前它始终为 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 或其他数字。
即:10/25 = 0
10.0/25.0 = 0.40
Objective-C 提供了 modulus
运算符 %
来确定余数,对于除整数很有用。
关于ios - NSURLConnection didSendBodyData 进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24189092/