繁体   English   中英

使用SDWebImage将文档目录中的本地映像加载到tableview

[英]Loading local images from documents directory to tableview using SDWebImage

我正在使用tableview从文档目录加载图像,创建缩略图并在tableview中显示它。 但是,我有一个问题:它变得很慢并且因为照片很大而使用相机拍摄时崩溃。

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

这个问题不容易回答,因为问题被广泛提出但我会尽我所能。

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

希望有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM