UITableView中的图像会在滚动时不断重新加载和错误的图像闪烁 [英] Images in UITableView keep re-loading and wrong images flash while scrolling

查看:79
本文介绍了UITableView中的图像会在滚动时不断重新加载和错误的图像闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个UITableView,它从URL请求中填充每个单元格。我使用'dispatch_queue'来阻止UItableView冻结。出于某种原因,当我滚动浏览UITableView时,图像会闪烁并消失并填充错误的单元格一秒钟,然后自行修复。这是我的代码。我使用restkit提取订阅源

i have created a UITableView which populates each cell from URL requests. I have used 'dispatch_queue' to prevent the UItableView from freezing. For some reason when i scroll through the UITableView the images flash and disappear and fill the wrong cell for a second until fixing themselves. Here is my code. I am pulling the feed using restkit

customCell.customCellTextLabel.text = [NSString stringWithFormat:@"%@",feedO.title];

    NSString *string = [NSString stringWithFormat:@"%@",feedO.body];

    NSURL *urlString;

        NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
        NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
        for (NSTextCheckingResult *match in matches) {
            if ([match resultType] == NSTextCheckingTypeLink) {
                urlString = [match URL];
                NSLog(@"found Body URL: %@ and title %@", urlString,feedO.title);
            }
        }

        dispatch_queue_t imageQueue = dispatch_queue_create("imageDownloader",nil);
        dispatch_async(imageQueue, ^{

            NSData *data = [[NSData alloc] initWithContentsOfURL:urlString];

            dispatch_async(dispatch_get_main_queue(), ^{

                customCell.customCellImageView.image = [UIImage imageWithData: data];
            });
        });


    return customCell;


推荐答案

你应该考虑使用这个库SDWebImage,可以在这里找到<针对这类问题,请使用href =https://github.com/rs/SDWebImage =nofollow> https://github.com/rs/SDWebImage 。它非常容易处理远程图像的异步下载和缓存。

You should consider using this library SDWebImage, available here https://github.com/rs/SDWebImage for that kind of problems. It handles asynchronous download and cache for remote images very easily.

最简单的安装是通过使用CocoaPods完成的

The simpliest installation is done by using CocoaPods

CocoaPods( http://cocoapods.org )是Objective-C的依赖管理器,它自动化并简化了使用过程项目中的第三方库。
有关详细信息,请参阅使用入门部分。

CocoaPods (http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the Get Started section for more details.

在您的Podfile中使用

Use in your Podfile

platform :ios, '6.1'
pod 'SDWebImage', '~>3.6'

在安装SDWebImage的依赖项之后,只需在视图控制器中使用以下行:

After installing the dependency for SDWebImage, just simply use the following lines in your view controller :

#import <SDWebImage/UIImageView+WebCache.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

这篇关于UITableView中的图像会在滚动时不断重新加载和错误的图像闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆