带图像的UITableView在滚动时显示错误的图像片刻

bir*_*age 0 objective-c uitableview nimbus ios

我有一个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;
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

Dun*_*n C 14

其他答案告诉你该怎么做,但不是为什么.

想想一个像你必须在医生办公室候诊室填写表格的牢房.想象一下,办公室重复使用这些表格,每个患者在填写信息之前必须清除表格上的所有数据.

如果您没有任何过敏症,您可能会跳过过敏症部分,因为它们不适用于您.但是,如果最后一个人有过敏症,并且你没有删除他们的答案,他们的答案仍将显示在表格上.

同样,当您将已回收的单元格出列时,您必须清除所有字段,甚至是那些不适用于您的字段.您应该将图像设置为nil或其起始占位符值.

请注意,如果以异步方式加载数据,则仍应首先将字段重置为其默认值,因为旧值将显示在异步加载完成之前.这就是你的情况.

您可以将图像设置为nil/placeholder in cellForRowIndexPath或in prepareForReuse,但是您需要在其中一个位置重置它,否则您将看到剩余的图像,直到新的图像完成加载.


小智 5

在您的自定义单元格中写下:

- (void)prepareForReuse {
   [super prepareForReuse];

   self.uiImage.image = nil;
} 
Run Code Online (Sandbox Code Playgroud)

  • 虽然此代码片段可能会解决问题,但它并没有解释为什么或如何回答问题。请包括对您的代码的解释,因为这确实有助于提高您的帖子质量。请记住,您是在为以后的读者回答问题,而那些人可能不知道您提出代码建议的原因 (2认同)