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;
}
setImage 메서드가 기본 스레드 (UI 스레드)에서 기본적으로 호출 되었습니까? 위 코드가 작동하고 thumbnailaImageView 요소의 image 속성을 설정하는지 확인할 수 있습니다.
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