使用NSURLConnection上载时,将进度设置为UIProgressView [英] Set the progress to UIProgressView when upload with NSURLConnection

查看:84
本文介绍了使用NSURLConnection上载时,将进度设置为UIProgressView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to refresh the progress bar in an UIProgressView for an upload request with NSURLConnection. The goal is to refresh the progress bar while uploading a picture. After several searches, I managed to use the didSendBodyData of my connection delegate to check the progress like this :

- (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%该方法只被调用一次......

Everything works fine, but the problem is that this method is only called one time... So the bar stay at 0% for a moment, then go instantly to 100% without intermediate. I tried (with iOS6 develloper tool on iPhone) to set my connection to a slow edge connection to figure if it is just my upload that is too fast, but no, the upload take a while at 0, then go to 100% instantly and the method is called only one time...

请问有什么想法吗?谢谢 !我无法想办法解决这个问题...

Any idea please ? Thank you ! I can't figure how to fix that...

推荐答案

你应该真正阅读关于数字类型的C教程据推测这两个 totalBytesWritten totalBytesExpectedToWrite 是一个整数类型,所以将它们会引发截断 - 即是的,结果的小数部分将消失。除非结果为100%,否则积分部分始终为0,因此所有这些除法将导致零。尝试将一个或两个变量转换为 float double 以获得合理的结果。

You should really read a C tutorial on numerical types. Presumably both totalBytesWritten and totalBytesExpectedToWrite are an integer type, so dividing them will result in truncation - that is, the fractional part of the result will be gone. Unless the result is at 100%, the integral part is always 0, so all those divisions will result in zero. Try casting one or both of the variables to float or double to get sensible results.

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

Also, UIProgressView doesn't accept values between 0 and 100 by default but between 0 and 1. All in all, you should write

self.delegate.progressView.progress = ((float)totalBytesWritten / totalBytesExpectedToWrite);

它应该可以正常工作。

编辑:问题在于您尝试上传的数据太小而且无需细分到较小的块,因此只需调用此方法一次。如果您提供大量数据,那么它将只能以单独的部分发送,因此将多次调用进度处理程序回调。

the problem was that the data you were trying to upload was too small and it didn't need to be broken down to smaller chunks, so it was necessary to call this method only once. If you supply a great amount of data, then it will be able to be sent only in separate pieces, so the progress handler callback will be called multiple times.

这篇关于使用NSURLConnection上载时,将进度设置为UIProgressView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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