【问题标题】:Lazy Loading User Image UITableView - Updating the Image in Request Block not Working延迟加载用户图像 UITableView - 更新请求块中的图像不起作用
【发布时间】:2013-05-16 20:37:18
【问题描述】:

所以我正在尝试为自定义 UITableView (BubbleTableView) 延迟加载用户图片

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
                [request setHTTPMethod:@"GET"];

                AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                    NSLog(@"Success");
                    UIBubbleTableViewCell *cell = (UIBubbleTableViewCell *)[self.bubbleTableView cellForRowAtIndexPath:indexPath];
                    NSBubbleData *data = [[NSBubbleData alloc] init];
                    data = [cell data];
                    data.avatar = [UIImage imageNamed:@"send.png"];
                    [self.profileImages setObject:image forKey:[self.commUsers objectAtIndex:x]];
                    //Success
                    [cell setData:data];

                } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                    //Failed
                    NSLog(@"ERROR: %@", response);
                }];

                [operation start];

我正在尝试将头像更改为另一个图像,但我没有使用从网上提取的图像,而只是我在本地存储的图像(用于缩小图像更新问题的范围)。

所以如果我把

 UIBubbleTableViewCell *cell = (UIBubbleTableViewCell *)[self.bubbleTableView cellForRowAtIndexPath:indexPath];
                NSBubbleData *data = [[NSBubbleData alloc] init];
                data = [cell data];
                data.avatar = [UIImage imageNamed:@"send.png"];
                [self.profileImages setObject:image forKey:[self.commUsers objectAtIndex:x]];
                //Success
                [cell setData:data];

在 AFImageRequestOperation 块内,图像不会更新。但是,如果我将完全相同的代码放在块之外,它会更新图像。我觉得我在 Blocks 的工作方式上遗漏了一些东西。我该如何解决这个问题?

谢谢!

【问题讨论】:

    标签: objective-c uitableview lazy-loading objective-c-blocks afnetworking


    【解决方案1】:

    尝试在主线程的block中运行UI代码:

    if ([NSThread isMainThread]) {
        // We're currently executing on the main thread.
        // We can execute the block directly.
        createBubbleTableViewCell();
      } else {
       //non-blocking call to main thread
       dispatch_sync(dispatch_get_main_queue(), createBubbleTableViewCell);
    }
    

    检查你是否在主线程上只是为了防止死锁。您也可以使用 dispatch_async 来阻止调用。

    UI 代码应始终在主线程上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 2011-10-26
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多