0

我正在使用 GCD 下载 UITableView 的标题图像。

dispatch_async的时候根本看不到图片,用dispatch_sync的时候还是同步下载。我该如何解决 ?

eventDetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
eventDetailsTable.dataSource = self;
eventDetailsTable.delegate = self;

[self.view addSubview:eventDetailsTable];

NSString *headerImageUrl = [NSString stringWithFormat:@"%@%@", [currentEvent objectForKey:@"baseurl"], [currentEvent objectForKey:@"sessionimage"]];
NSURL *headerImageURL = [NSURL URLWithString:headerImageUrl];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:headerImageURL];
    UIImage *headerImage = [UIImage imageWithData:imageData];
    UIImageView *headerImageView = [[UIImageView alloc] initWithImage:headerImage];
    eventDetailsTable.tableHeaderView = headerImageView;
});
4

1 回答 1

0

更新 UI 时,必须在主线程上进行。所以这里是解决方案:

dispatch_async(global_queue, ^{
    //Do your work

    dispatch_async(dispatch_get_main_queue(), ^{
        //Update UI
    });

});
于 2013-04-19T01:17:34.470 回答