【发布时间】: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);
});
}
当我运行这段代码时
UIProgressView 永远不会出现
我得到的日志是
Downloading 0% complete。我希望得到正在下载的图像的日志。
我还认为我应该在viewDidDownload 中使用alloc 和init 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