Suggerimenti sull'immagine asincrona Caricamento per l'intecilazione Vista in IOS
-
11-12-2019 - |
Domanda
Attualmente sto caricando il mio tavolo con una miniatura a sinistra, titolo e sottotitolo utilizzando questo codice:
- (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;
}
.
Certo che questo non funzionerà in produzione a causa del caricamento sincrono che sto facendo qui.
Qual è il tuo suggerimento per il caricamento asincrono di immagini in una situazione come questa?
Ho trovato AF Networking (non l'ho ancora usato) ma mi chiedo se c'è un altroApproccio leggero a questo problema.
Soluzione
AsyncImageView
è il tuo amico, controlla qui .
Basta impostare il imageURL
e siete bene andare.
Altri suggerimenti
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;
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow