【问题标题】:UIProgressView does not show upUIProgressView 不显示
【发布时间】:2013-08-27 03:59:03
【问题描述】:

我有一个从服务器下载图片的应用。我想向用户展示进度。我在苹果文档上阅读了有关 UIProgressView 的信息,并在这个网站上阅读了许多答案,但我无法让它工作。这是我的代码 在我的viewDidLoad 我有

_profileProgressView.hidden= YES;
_profileProgressView.progress = 0.0;

在我的- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 中,我显示了 UIProgressView,然后获得了预期的图像大小。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
      self.profileProgressView.hidden= NO;
      [self.remote_response setLength:0];
      received = 0;
      self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
      NSLog(@"Content Length::%@", self.filesize);
  }

在我的- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d 中,我计算下载图像的百分比,然后更新 UIProgressView 的进度并记录下载进度。

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
        [self.remote_response appendData:d];

        NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.remote_response length]];
        dispatch_async( dispatch_get_main_queue(), ^ {
              float progress = (_profileProgressView.progress + ([self.filesize floatValue]/[resourceLength floatValue])*100);
              [self.profileProgressView setProgress:progress animated:YES];
              NSLog(@"Downloading %.0f%% complete", _profileProgressView.progress);
        });
   }

当我运行这段代码时

  1. UIProgressView 永远不会出现

  2. 我得到的日志是Downloading 0% complete。我希望得到正在下载的图像的日志。

我还认为我应该在viewDidDownload 中使用allocinit UIProgressView,当我这样做时,我得到了UIProgressView,但它不显示进度并且在图像下载完成后它不会隐藏。

【问题讨论】:

  • 为什么要分派到这里的主队列?委托响应在主线程上调用,因此您不必这样做。另外,进度是从0到1.0,所以我不知道你为什么要乘以100。另外,你是在故事板上拖出来的吗?那么就不需要alloc和init了。
  • 检查progressView对象是否为nil。
  • 你完成了吗[self.view bringSubviewToFront: _profileProgressView]; ?? @潘迪
  • @AdamG 一开始我没有使用调度,但是当它不起作用时我添加了它,因为我在 StackOverflow 上的某个地方看到,也没有乘法,在示例代码中看到它来自苹果文档,是的,在故事板上拖拽
  • @ChalamphongPandey 您是否务实地添加 UIPreogressView 然后您是否完成了 [self.view addSubView _profileProgressView] 或者如果您已将其放入 xib 或 Storybaord 那么您是否合成(@synthasize)进度视图? ?

标签: ios objective-c uiprogressview


【解决方案1】:

我认为您使用 NSNumber 对数据类型进行装箱和拆箱使数学变得过于复杂。

我要做的是创建三个变量:

double fileLength;
double lastProgress;
double currentLength;

然后做简单的数学运算:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  fileLength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
  double length = [d length];
  currentLength += length;
  double progress = currentLength/fileLength;

  if (lastProgress < progress) {
    profileProgressView.progress = progress;
    lastProgress = progress;
  }
}

【讨论】:

  • 使用你的代码我 NSLog lastProgress 我立即得到 1.0。
  • opps...错过了一行。长度应为 [d 长度] 作为双精度...已编辑。
  • 我仍然得到同样的东西,但我认为我遇到了这个问题,因为我的图像太小了
  • 是的,这行得通,只是图像太小了。用非常慢的互联网获取了一个稍微大一点的图像,并看到了这个工作。谢谢
【解决方案2】:

我之前也遇到过同样的问题。在这种情况下,我们无法显示准确的进度视图。为了显示准确的进度,您的服务器应提供在响应标头中发送的字节。然后,你可以在你的连接中获得确切的数据:didReceiveData:delegate。否则,您将立即获得 1.0。

即使你想在这种情况下显示进度视图而不依赖于服务器,你也可以通过将数据的长度分成 n 个步骤并使用 NSTimer 来实现它。

【讨论】:

  • 标题Content-Length 不是已经有总大小了吗?
猜你喜欢
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多