0

Привет, я использую GCD для загрузки изображения в uitableview. Теперь проблема заключается в анимации изображения. Я назвал три изображения с сервера он загружает в клетку, но я получил 3 проблемыЗагрузка изображения Анимация с использованием GCD

1) Одиночное изображение повторяется во всех трех клетках

2) первые и последние изображения клеток мигания и изменения

3) когда изображение изменилось, оно повторяется во всех ячейках.

Даже если я не давал анимации ни еще одушевляет т.е. Мигание

Код:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 

[imageArray retain]; 

//get a dispatch queue 
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
//this will start the image loading in bg 
dispatch_async(concurrentQueue, ^{ 
    NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]]; 

    //this will set the image when loading is finished 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.imageView.image = [UIImage imageWithData:image]; 

     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
     }); 
    }); 

return cell; 

}

ответ

1

Вы не должны загружать изображения в этом месте.

Если у вас слишком много строк, сделайте это, например. viewDidLoad и сохранить загруженные изображения в новый изменяемый массив.

Что-то вроде этого:

- (void) viewDidLoad { 
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); 
dispatch_async(queue, ^{ 
     int row = 0; 
     self.thumbnails = [NSMutableArray array]; 
     for (NSString *imageURLString in imageArray) { 
     //Your way of download images 
     NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLString]]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.thumbnails addObject:image]; 
      NSArray *indexes = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:1]]; 
      [self.tableViewOutlet reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone]; 
     }); 
     row++; 
    } 
}); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (self.thumbnails && [self.thumbnails count] > indexPath.row) { 
      cell.attachmentImageView.image = [self.thumbnails objectAtIndex:indexPath.row]; 
     } else { 
      cell.attachmentImageView.image = nil; 
     } 
} 
+0

Благодаря человеку. Правильно работает –

+0

Это был мой старый код. Я только что исправил это. – Tobol

Смежные вопросы