ios - 使用 SDWebImage 将本地镜像从文档目录加载到 TableView

标签 ios performance tableview local sdwebimage

我正在使用一个 tableview,它从文档目录加载图像,创建一个缩略图并将其显示在 tableview 中。但是,我遇到了一个问题:由于使用相机拍摄的图片很大,它变得缓慢并崩溃。

我探索了包括 GCD 在内的几种解决方案来在后台线程中完成工作,但结果是一样的。所以,我想看看 SDWebImage 但我不知道它是否也适用于本地文件,在这种情况下不适用于网络图像。有人可以告诉我吗?如果不是,这个问题是如何解决的?是否有可以帮助解决此问题的 API?

最佳答案

这个问题不容易回答,因为这个问题问得相当广泛,但我会尽力而为。

首先,如果我有昂贵的处理要做,我通常会分派(dispatch)一个后台线程,以免阻塞主线程,这非常重要。 我真的不知道你为什么不使用普通的 UIImageView 来做你正在做的事情,而是尝试实现以下内容:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"YourCell";
    MyCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
/*
 Whatever Code you want 
*/
    NSArray* params =@[cell.myImageView, @"http://myfancyimages.com/image.png"];
    [self performSelectorInBackground:@selector(loadEventImageWithParameters:) withObject:params];
    return cell;
}

现在添加函数:

- (void) loadEventImageWithParameters:(id) parameters {
    NSArray* params = [[NSArray alloc] initWithArray:(NSArray*)parameters];
    NSURL *url = [NSURL URLWithString:[params objectAtIndex:0]];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
    UIImageView* theImageView = (UIImageView*) [params objectAtIndex:0];
    [theImageView setImage:image];
}

如果您要加载大量图片,建议您对流程进行排队,这样您就不会“窃取”Grand Central Dispatch 的所有资源。 通读这篇优秀的文章 http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial了解更多详情。

希望对您有所帮助

关于ios - 使用 SDWebImage 将本地镜像从文档目录加载到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792704/

相关文章:

iphone - 我仍然收到 "The executable was signed with invalid entitlements."错误

ios - 在 iOS 的 SWRevealController 中保持 View Controller 的保存状态?

java - 了解 MapReduce 性能?

swift - tableview 继续返回选中的第一个单元格 [swift]

JavaFX - 如何更改选定未聚焦行的 TableView 颜色?

ios - 创建一个 Sprite 并给它一个自定义类

ios - 无法调整 viewController 的自动调整大小蒙版

performance - ffmpeg 在没有 avformat_find_stream_info 的情况下解码慢速调用

mysql - 在大型表上使用多个 where 条件提高 mysql 查询的性能

ios - UIWeb View 从数组加载错误的地址