【问题标题】:UITableView header image download using GCD使用 GCD 下载 UITableView 标题图片
【发布时间】:2013-04-19 00:44:28
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c grand-central-dispatch


    【解决方案1】:

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

    dispatch_async(global_queue, ^{
        //Do your work
    
        dispatch_async(dispatch_get_main_queue(), ^{
            //Update UI
        });
    
    });
    

    【讨论】:

    • 我试过了。它可以在模拟器中运行,但只有 50% 的时间在设备上运行
    猜你喜欢
    • 2011-12-12
    • 2017-08-08
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    相关资源
    最近更新 更多