2017-09-18 11 views
0

많은 행이 있고 모든 행에 이미지 만있는 UITableView이 있습니다. 지연을 막기 위해 아래 코드를 사용했습니다. 하지만 이제는 스크롤하면서 이전 행의 사진을 잠깐 동안 표시 한 다음 바로 수정합니다. 그 문제를 해결하는 방법? 사용자 정의 셀에스크롤하는 동안 이미지가있는 UITableView가 잠시 동안 잘못된 이미지를 표시합니다.

- (void)loadImageNamed:(NSString *)name { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ 
     // Determine path to image depending on scale of device's screen, 
     // fallback to 1x if 2x is not available 
     NSString *pathTo1xImage = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"]; 

     NSString *pathToImage = pathTo1xImage; 

     UIImage *uiImage = nil; 

     if (pathToImage) { 
      // Load the image 
      CGDataProviderRef imageDataProvider = CGDataProviderCreateWithFilename([pathToImage fileSystemRepresentation]); 
      CGImageRef image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault); 


      // Create a bitmap context from the image's specifications 
      // (Note: We need to specify kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little 
      // because PNGs are optimized by Xcode this way.) 
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
      CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetWidth(image) * 4, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); 


      // Draw the image into the bitmap context 
      CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); 

      // Extract the decompressed image 
      CGImageRef decompressedImage = CGBitmapContextCreateImage(bitmapContext); 


      // Create a UIImage 
      uiImage = [[UIImage alloc] initWithCGImage:decompressedImage]; 


      // Release everything 
      CGImageRelease(decompressedImage); 
      CGContextRelease(bitmapContext); 
      CGColorSpaceRelease(colorSpace); 
      CGImageRelease(image); 
      CGDataProviderRelease(imageDataProvider); 
     } 


     // Configure the UI with pre-decompressed UIImage 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.categoryImageLabel.image = uiImage; 
     }); 
    }); 
} 
+0

"prepareforreuse"를 사용해 보셨습니까? –

답변

7

전화에 그들을 = 전무를 이미지에 대한 자리 표시자를 사용하거나 만들 , 그러나 이유는 아닙니다.

의사 대기실에서 작성해야하는 양식과 같은 셀을 생각해보십시오. 사무실에서 해당 양식을 다시 사용한다고 가정하고 각 환자는 정보를 작성하기 전에 양식의 모든 데이터를 지워야합니다.

알레르기가없는 사람에게는 적용되지 않으므로 알레르기 란을 건너 뛸 수 있습니다. 그러나 마지막 사람이 알레르기가 있었고 응답을 지우지 않았다면 답장은 계속 양식에 표시됩니다.

마찬가지로 재활용 된 셀을 큐에서 제거 할 때는 모든 필드를 지우고, 적용되지 않는 필드도 지워야합니다. 이미지를 nil 또는 시작 자리 표시 자 값으로 설정해야합니다.

데이터를 비동기 적으로로드하는 경우 비동기로드가 완료 될 때까지 이전 값이 표시되므로 필드를 기본값으로 다시 설정해야합니다. 그것은 귀하의 경우에 일어나는 일입니다.

cellForRowIndexPath 또는 prepareForReuse의 이미지를 nil/자리 표시 자로 설정할 수 있지만 그 중 하나에서 재설정해야합니다. 그렇지 않으면 새 이미지가로드 될 때까지 나머지 이미지가 표시됩니다.

+0

좋은 답변입니다! +1 나를 –

+0

좋은 대답 !!! 항상 이해하기 쉬운 예제로 설명하십시오 –

+0

감사합니다. 모든 것을 설명합니다. – birdcage

2

이 쓰기 :

- (void)prepareForReuse { 
    [super prepareForReuse]; 

    self.uiImage.image = nil; 
} 
+1

이 코드 단편은 문제를 해결할 수 있지만 질문에 대한 답변의 이유 또는 설명은 설명하지 않습니다. 게시물의 품질을 향상시키는 데 도움이되는 코드에 대한 설명을 포함하십시오. 앞으로 독자의 질문에 대답하고 있으며 코드 제안에 대한 이유를 모를 수도 있습니다. –

0

무엇을 할 수있는 다른 답변은 당신에게

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = CustomUITableViewCell 
    cell.imageview.image = nil 
    // load your image 
}