NSURLConnection didSendBodyData进度 [英] NSURLConnection didSendBodyData progress

查看:233
本文介绍了NSURLConnection didSendBodyData进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用POST请求将一些数据上传到服务器,并且尝试基于NSURLConnectiondidSendBodyData方法的totalBytesWritten属性更新UIProgressView的进度.使用下面的代码,我无法正确更新进度视图,直到完成,它始终为0.000.我不确定要乘以或除以什么来获得更好的上传进度.

I'm using a POST request to upload some data to a server, and I'm trying to update the UIProgressView's progress based on the totalBytesWritten property of the didSendBodyData method of NSURLConnection. Using the below code, I don't get a proper updating of the progress view, it's always 0.000 until it finishes. I'm not sure what to multiply or divide by to get a better progress of the upload.

我将不胜感激!代码:

- (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值以进行除法.

You have to cast the bytesWritten and the bytesExpected as float values to divide.

float myProgress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
progressView.progress = myProgress;

否则,除以2的整数将得到0或其他数字.

Otherwise you are will get either a 0 or some other number as a result of dividing 2 integers.

即:10 / 25 = 0

10.0 / 25.0 = 0.40

Objective-C提供了modulus运算符%来确定余数,对于除以整数非常有用.

Objective-C provides the modulus operator % for determining remainder and is useful for dividing integers.

这篇关于NSURLConnection didSendBodyData进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆