iOS에서 UITableView를위한 비동기 이미지로드에 대한 제안
-
11-12-2019 - |
문제
현재이 코드를 사용하여 왼쪽의 섬네일, 제목 및 자막이있는 테이블을로드하고 있습니다.
- (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;
}
.
물론 이것은 여기서 수행중인 동기 로딩으로 인해 생산에서는 효과가 없습니다.
이런 상황에서 이미지의 비동기 로딩에 대한 귀하의 제안은 무엇입니까?
AF 네트워킹 (아직 사용하지 않았습니다)하지만 더 이상 존재하는지 궁금해합니다.이 문제에 대한 가벼운 접근 방식.
다른 팁
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;
}
. 제휴하지 않습니다 StackOverflow