我創建了一個繼承自NSOperation的fetchImageOperation類。它使用URL獲取圖像,然後在完成時觸發該塊。我的問題是,如果我有以下代碼:NSOperation獲取圖像和更新UITableView UIImageView屬性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Customer *customer = [_customers objectAtIndex:[indexPath row]];
FetchImageOperation *imageOperation = [[FetchImageOperation alloc] initWithURL:[NSURL URLWithString:vegetable.imageURL]];
cell.nameLabel.text = customer.name;
imageOperation.fetchImageOperationCompletionBlock = ^(UIImage *image)
{
[cell.thumbnailImageView setImage:image];
};
[_queue addOperation:imageOperation];
return cell;
}
被稱爲主線程(UI線程),在默認情況下在setImage方法?我可以確認上面的代碼有效,它確實設置了thumbnailaImageView元素的圖像屬性。
FetchImageOperation.m文件:
@implementation FetchImageOperation
-(id) initWithURL:(NSURL *)url
{
self = [super init];
self.url = url;
return self;
}
-(void) main
{
NSData *data = [NSData dataWithContentsOfURL:_url];
UIImage *image = [UIImage imageWithData:data];
if(self.fetchImageOperationCompletionBlock != nil)
{
self.fetchImageOperationCompletionBlock(image);
}
}
@end