1

我正在向 URL 发出请求以下载图像。我将它作为NSData对象接收,但是当我尝试将其转换UIimage with imageWithData:UIImage未加载时。这是我的代码

__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__weak typeof(request)weakRequest = request;
__block UIImage * imager;
__block NSData * image;
[request setCompletionBlock:^{
    [self parseXMLFileAtFileId:[weakRequest responseData]];
    if (!files){
        cell.correctImage.image = [UIImage imageNamed:@"sidebarPlaceholder"];
    }
    else{

        NSURL *url = [NSURL URLWithString:[self downloading:[files objectAtIndex:0]]];
        __block ASIHTTPRequest *requester = [ASIHTTPRequest requestWithURL:url];

        __weak typeof(request)weakRequesting = requester;
        [requester setCompletionBlock:^{
            image =  [weakRequesting responseData];
            imager = [UIImage imageWithData:image];
            cell.correctImage.image = imager;
            [cell layoutIfNeeded];
        }];

        [requester startAsynchronous];
    }
}];

以前我通过明确UIImageView说明UITableviewCell. 如何让图像显示?

4

1 回答 1

1

尝试在主线程中运行这些代码块,因为您正在尝试在后台线程中设置图像和更新布局。之后立即使用以下NSThread方法

UIImage *imager = [UIImage imageWithData:image]

[self performSelectorOnMainThread:@selector(updateImage:) withObject:imager waitUntilDone:false];

// 定义更新图像的方法

- (void)updateImage: (UIImage *)paramImage {
    cell.correctImage.image = paramImage;
    [cell layoutIfNeeded];
}
于 2013-06-28T21:15:15.643 回答