簡體   English   中英

在IOS中使用GCD下載圖像時如何更新標簽文本

[英]How to update Label text when downloading Images using GCD in IOS

我使用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];
        });
    });

移動

dispatch_async(dispatch_get_main_queue(), ^{
    //Here you returns to main thread.
    [downloadLabel setText:text];
});

for循環中,以便每次下載后更新UI(而不是僅在所有迭代結束時更新)。

在dispatch_async塊中嘗試以下代碼:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
   [downloadLabel setText:text];
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM