2012-02-17 2 views
1

setNeedsDisplay을 호출하기 위해 이미지를 비동기 적으로 다운로드하는 블록에서 다음과 같은 것을 사용하는 것이 좋습니다. 메인 스레드와 그것을 빨리 표시 할 수 있습니다.cellForRowAtIndexPath : 메인 스레드에서 호출해도 백그라운드에서 다운로드 한 이미지에 setNeedsDisplay가 작동하지 않습니다.

dispatch_async(main_queue, ^{ 
       [view setNeedsDisplay]; 
      }); 

아래에서 볼 수있는 것처럼이 작업을 시도하고 있지만 이미지는 다운로드되는 즉시 표시되지 않습니다. 일반적으로 약 4-5 초 지연이 있습니다. 특정 행을 선택하면 이미지가 나타나고 다른 행은 계속 표시되지 않기 때문에이 사실을 알 수 있습니다.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

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

    UIImageView *iv = (UIImageView*)[cell viewWithTag:kCellSubViewWavImageView];; 

     //async for scroll performance 
     dispatch_queue_t queue = 
     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
     dispatch_async(queue, ^{ 

      NSURL *url = [[NSURL alloc] initWithString:[self.user.favWavformURLAr objectAtIndex:indexPath.row]]; 
      NSLog(@"background"); 
      NSData *imageData = [[NSData alloc] initWithContentsOfURL:url]; 
      UIImage *image = [[UIImage alloc] initWithData:imageData]; 

      iv.image = image; 

      dispatch_queue_t main_queue = dispatch_get_main_queue(); 
      dispatch_async(main_queue, ^{ 
       NSLog(@"main thread"); 
       [iv setNeedsDisplay]; 
      }); 

     }); 

    } 

    return cell; 
} 

또한, NSLog(@"background");NSLog(@"main thread"); 호출 아래 내가 기대하는 것입니다 초기 6 개 셀의 호출에 대한 다음과 같은 순서로 인쇄됩니다, 나는 생각한다. 하지만 여전히 작동하지 않습니다.

2012-02-17 20:46:27.120 SoundcloudFavs[8836:1c03] background 
2012-02-17 20:46:27.169 SoundcloudFavs[8836:1b03] background 
2012-02-17 20:46:27.170 SoundcloudFavs[8836:6b07] background 
2012-02-17 20:46:27.173 SoundcloudFavs[8836:7503] background 
2012-02-17 20:46:27.174 SoundcloudFavs[8836:7103] background 
2012-02-17 20:46:27.177 SoundcloudFavs[8836:8003] background 
2012-02-17 20:46:27.219 SoundcloudFavs[8836:207] main thread 
2012-02-17 20:46:27.270 SoundcloudFavs[8836:207] main thread 
2012-02-17 20:46:27.282 SoundcloudFavs[8836:207] main thread 
2012-02-17 20:46:27.285 SoundcloudFavs[8836:207] main thread 
2012-02-17 20:46:27.296 SoundcloudFavs[8836:207] main thread 
2012-02-17 20:46:27.300 SoundcloudFavs[8836:207] main thread 

아이디어가 있으십니까?

답변

3

메인 스레드에 다운로드 한 이미지를 설정하십시오.

dispatch_async(main_queue, ^{ 
      NSLog(@"main thread"); 
      iv.image = image; 
     }); 

또한 세포하지만 cell.contentView 아니 파단으로 이미지를 추가하는 것이 좋습니다.

+0

대단히 고마워요. 내가 그것을 생각하지 않았다는 것을 믿을 수 없다. – Remover