使用SDWebImage将本地图像从文档目录加载到tableview
问题描述:
我使用的tableview从文档目录加载图像,创建缩略图并在tableview中显示它。但是,我遇到了一个问题:由于照片很大,使用相机拍摄时会变得缓慢并且崩溃。使用SDWebImage将本地图像从文档目录加载到tableview
我已经探索了几种解决方案,包括GCD做的工作在后台线程,但结果是一样的东西。所以,我想看看SDWebImage,但我不知道它是否也适用于本地文件,而不是这种情况下的网络图像。有人可以提醒我吗?如果不是,这个问题如何解决?有没有可以帮助解决这个问题的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 [email protected][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];
}
如果你有一个很多图片加载你,建议排队你的进程,所以你不要“窃取”大*调度的所有资源。 请仔细阅读此优秀帖子http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial以了解更多详情。
希望能帮到
感谢您的回答。是的,我正在设计数百个大型文件,并且我最终实施了GCD解决方案,并取得了积极成果。 –