我使用GCD从服务器下载图像并更新UILabel上的处理然后将标签打印到屏幕上(例如:它将打印到屏幕上:"下载:3/15图像")
但最初标签是:"正在下载:0/15图像"。然后当它完成下载时,标签为"下载:15/15图像"。用户无法看到下载处理。
我想要的是用户可以看到如下处理: "下载:1/15图像","下载:2/15图像"。"下载:3/15图像",...,"下载:15/15图像"。
这是我的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Here your non-main thread.
NSString *text;
for (int i = 0;i<[self.pageImages count];i++){
NSString *image = [self.pageImages objectAtIndex:i];
[dataManage downloadImagesFromUrl: image ];
text = [NSString stringWithFormat:@“Downloading %d/%d”,i,self.pageImages.count];
}
dispatch_async(dispatch_get_main_queue(), ^{
//Here you returns to main thread.
[downloadLabel setText:text];
});
});
答案 0 :(得分:2)
移动
dispatch_async(dispatch_get_main_queue(), ^{
//Here you returns to main thread.
[downloadLabel setText:text];
});
在for
循环内部,以便在每次下载后更新UI(而不是仅在所有迭代结束时)。
答案 1 :(得分:1)
在dispatch_async块中尝试此代码:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[downloadLabel setText:text];
}];