ios - NSURLConnection didSendBodyData 进度

标签 ios objective-c nsurlconnection uiprogressview nsurlconnectiondelegate

我正在使用 POST 请求将一些数据上传到服务器,我正在尝试根据 didSendBodyDatatotalBytesWritten 属性更新 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/

相关文章:

objective-c - iOS6 Facebook : What to do if user has not configured Facebook?

iphone - 是否可以推送到另一个 View Controller ,然后从该 View Controller 中访问一个方法?

iOS:从 CNContact 获取数字 key

iphone - 如何创建类似 NSURLConnection 的东西?

iphone - NSURLConnection sendSynchronousRequest - 后台到前台

ios - NSURLConnection didReceiveData 未加载数据

ios - 如何在 Metal 中组合使用不同着色器的渲染命令编码器

ios - 事件指示器未显示在 PDF uiwebview 加载中

objective-c - 如何将 NSData 转换为 NSString?

ios - 我如何知道应用程序的启动时间?