Tengo un UITableView con celdas personalizadas. Cargo imágenes de forma asíncrona usando Grand Central Dispatch. Todo funciona bien, pero cuando me desplazo hacia abajo, las imágenes cargadas anteriormente se muestran hasta que se descarga la nueva imagen. Aquí está mi código:GCD UITableVer imágenes de carga asíncrona, las celdas incorrectas se cargan hasta que se descargue la nueva imagen
if (![[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:@"image.png"]])
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url=[pat stringByAppendingPathComponent:@"comments.txt"];
NSString *u=[NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *image=[NSData dataWithContentsOfURL:imageURL];
[image writeToFile:[pat stringByAppendingPathComponent:@"image.png"] atomically:YES];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]];
[cell setNeedsLayout];
NSLog(@"Download");
});
});
}
else
{
NSLog(@"cache");
cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]];
}
Cualquier sugerencia apreciada. P.S. Reutilizo las celdas
gracias por su respuesta ... pero, ¿podría proporcionar algún código de ejemplo? – blackhawk4152
Actualizado mi respuesta – hypercrypt
gracias, hombre! justo lo que quería – blackhawk4152