【问题标题】:Loading local images from documents directory to tableview using SDWebImage使用 SDWebImage 将本地图像从文档目录加载到 tableview
【发布时间】:2012-09-29 08:26:38
【问题描述】:

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

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

【问题讨论】:

    标签: ios performance tableview local sdwebimage


    【解决方案1】:

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

    首先,如果我要进行昂贵的处理以不阻塞主线程,我通常会调度一个后台线程,这非常重要。 我真的不知道你为什么不使用普通的 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 了解更多详情。

    希望有所帮助

    【讨论】:

    • 感谢您的回答。是的,我正在为数百个大文件进行设计,我最终使用 GCD 实现了一个解决方案,到目前为止取得了积极的成果。
    猜你喜欢
    • 2018-08-05
    • 2012-01-27
    • 1970-01-01
    • 2023-03-25
    • 2010-11-04
    • 1970-01-01
    • 2023-03-22
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多