Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用NSURLConnection上载时,将进度设置为UIProgressView_Ios_Ios5_Ios6_Nsurlconnection_Nsurlconnectiondelegate - Fatal编程技术网

Ios 使用NSURLConnection上载时,将进度设置为UIProgressView

Ios 使用NSURLConnection上载时,将进度设置为UIProgressView,ios,ios5,ios6,nsurlconnection,nsurlconnectiondelegate,Ios,Ios5,Ios6,Nsurlconnection,Nsurlconnectiondelegate,我正在尝试刷新UIProgressView中的进度条,以获取带有NSURLConnection的上载请求。目标是在上传图片时刷新进度条。经过几次搜索后,我设法使用我的连接委托的didSendBodyData检查进度,如下所示: - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten

我正在尝试刷新UIProgressView中的进度条,以获取带有
NSURLConnection
的上载请求。目标是在上传图片时刷新进度条。经过几次搜索后,我设法使用我的连接委托的
didSendBodyData
检查进度,如下所示:

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    if([self.delegate respondsToSelector:@selector(progressView)])
    {
        self.delegate.progressView.progress = (totalBytesWritten / totalBytesExpectedToWrite) * 100.0;
    }
}
一切正常,但问题是这个方法只调用一次。。。因此,酒吧停留在0%一刻,然后立即到100%,没有中间。我尝试(在iPhone上使用iOS6 develloper工具)将我的连接设置为慢速边缘连接,以确定是否只是我的上传速度太快,但没有,上传在0时需要一段时间,然后立即转到100%,该方法只调用一次


有什么想法吗?非常感谢。我不知道如何解决这个问题……

你应该真正阅读一本关于数字类型的C教程。大概
TotalBytesWrite
totalBytesExpectedToWrite
都是整数类型,所以将它们除以将导致截断-也就是说,结果的小数部分将消失。除非结果为100%,否则整数部分始终为0,因此所有这些除法的结果均为零。尝试将一个或两个变量强制转换为
float
double
,以获得合理的结果

另外,
UIProgressView
默认情况下不接受介于0和100之间的值,而是接受介于0和1之间的值。总之,你应该写

self.delegate.progressView.progress = ((float)totalBytesWritten / totalBytesExpectedToWrite);
它应该很好用


编辑:问题是您试图上载的数据太小,不需要将其分解为较小的数据块,因此只需调用此方法一次。如果您提供了大量数据,那么它将只能以单独的部分发送,因此将多次调用进度处理程序回调。

感谢您的回答,是的,我理解我在这里的错误,但问题不在于此。。。我的行当然是假的,但问题是当我
NSLog
调用方法检查
bytesWrite
totalbytesWrite
totalBytesExpectedToWrite
时,我清楚地看到该方法只被调用了一次,所有值都等于
totalbytesWrite
,因此没有任何进展,只有0到100…@FabienParseError可能数据太小,无法切成小块。谢谢@H2CO3!就这样!我试着同时上传10张图片,现在进度条显示了中间人;)很抱歉,谢谢你的帮助!