Pregunta

Actualmente estoy cargando mi tabla con una miniatura, un título y un subtítulo del lado izquierdo usando este código:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = [posts objectAtIndex:indexPath.row];

    cell.textLabel.text       = [post objectForKey:@"post_text"];
    cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];

    NSString *postpictureUrl = [post objectForKey:@"picture"];
    NSData   *data           = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];

    cell.imageView.image = [UIImage imageWithData:data];

    return cell;
}

Por supuesto, esto no funcionará en producción debido a la carga sincrónica que estoy haciendo aquí.

¿Cuál es su sugerencia para la carga asincrónica de imágenes en una situación como esta?

He encontrado Redes AF (aún no lo he usado), pero me pregunto si existe un enfoque más ligero para este problema.

¿Fue útil?

Solución

AsyncImageView es tu amigo, compruébalo aquí.

Simplemente configura el imageURL y estás listo para irte.

Otros consejos

Use lazy loading in table view
Use this code


    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        NSDictionary *post = [posts objectAtIndex:indexPath.row];

        cell.textLabel.text = [post objectForKey:@"post_text"];
        cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
        dispatch_async(queue, ^{
         //This is what you will load lazily
      NSString *postpictureUrl = [post objectForKey:@"picture"];
        NSData   *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:postpictureUrl]];
            dispatch_sync(dispatch_get_main_queue(), ^{

        cell.imageView.image = [UIImage imageWithData:data];
                [cell setNeedsLayout];
      }); 
}); 

        return cell;
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top