我有一个 ImageDownloader 类,它具有使用 GCD 下载图像的功能:
- (void) downloadImageFromURL:(NSURL *) url completionBlock:(void (^)(UIImage *image, NSError *error)) block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *picture=[UIImage imageWithData:imageData];
if(picture) {
block(picture, nil);
}
else {
NSError *error = [NSError errorWithDomain:@"image_download_error" code:1
userInfo:[NSDictionary dictionaryWithObject:@"Can't fetch data" forKey:NSLocalizedDescriptionKey]];
block(nil, error);
}
});
}
我的 tableviewcell(在它自己的 nib 文件中定义)为每一行调用这个函数,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//custom cell in nib file
static NSString *MyIdentifier = @"contactsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"contactsCell" owner:self options:nil]; //feedCell je ime nib fajla
cell = contactsCell;
self.contactsCell = nil;
}
if (indexPath.section == 0) {
UIImageView *avatar=(UIImageView *) [cell viewWithTag:1];
avatar.image=[UIImage imageNamed:@"petek.jpg"];
UILabel *nameLabel= (UILabel *) [cell viewWithTag:2];
nameLabel.text=@"Some guy";
UIButton *askBtn=(UIButton *) [cell viewWithTag:3];
[askBtn setTitle:@"Ask" forState:UIControlStateNormal];
[askBtn addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
}
else {
UIImageView *avatar=(UIImageView *) [cell viewWithTag:1];
ImageDownloader *idl=[[ImageDownloader alloc] init];
NSURL *imageUrl=[NSURL URLWithString:@"http://someImageUrl"];
[idl downloadImageFromURL:imageUrl completionBlock:^(UIImage *image, NSError *error)
{
if(!error) {
dispatch_sync(dispatch_get_main_queue(), ^(void) {
avatar.image=image;
});
} else {
NSLog(@"error %@", error);
}
}];
UILabel *nameLabel= (UILabel *) [cell viewWithTag:2];
nameLabel.text=@"John Stockton";
UIButton *followBtn=(UIButton *) [cell viewWithTag:3];
[followBtn setTitle:@"Follow" forState:UIControlStateNormal];
[followBtn addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
第 1 部分的图像正确加载,但在滚动时,有时第 1 部分的一行中的图像仅在片刻之内有第 0 部分的图像。
我究竟做错了什么?