【发布时间】:2016-02-02 07:37:25
【问题描述】:
我正在使用coredata。我在coredata 中成功添加了图像,但是当我想显示列表时出现问题。当我向下滚动UITableView 时,我遇到了这个滞后问题。我读了这篇文章Loading image from CoreData at cellForRowAtIndexPath slows down scrolling。我仍然有延迟,现在我正在尝试使用LazyLoadImage,但问题是,他们没有使用viewdidload()。我不知道这个LazyLoadImage。我想详细了解 Grand Central Dispatch 以及它的工作原理。
这是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"imageCell";
NSManagedObject *coreData = [self.devices objectAtIndex:indexPath.row];
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *imagex = [cache objectForKey:[NSNumber numberWithUnsignedInteger:indexPath.row]];
UIImage * rowImage = [UIImage imageWithData:[coreData valueForKey:@"image"]];
UIImageView * myImage = [[UIImageView alloc] initWithImage:rowImage];
myImage.contentMode = UIViewContentModeScaleAspectFill;
myImage.frame = CGRectMake(0, 0, 79, 56);
cell.name.text = [coreData valueForKey:@"name"];
if (imagex == nil) {
[cache setObject:myImage.image forKey:[NSNumber numberWithUnsignedInteger:indexPath.row]];
imagex = [cache objectForKey:[NSNumber numberWithUnsignedInteger:indexPath.row]];
}
if(imagex){
dispatch_async(dispatch_get_main_queue(), ^{
if ([[tableView indexPathsForVisibleRows] containsObject:indexPath]) {
//check that the relevant data is still required
UITableViewCell * correctCell = [self.tableView cellForRowAtIndexPath:indexPath];
//get the correct cell (it might have changed)
[[correctCell imageView] setImage:imagex];
[correctCell setNeedsLayout];
}
});
}
});
return cell;
}
如果我将列表放入控制器中,性能是否重要?我这里没有使用 xib。
这里的问题只是第一次加载。当它向下滚动时。有些代码很熟悉。我正在尝试自己解决它,但现在没有运气。我已经在我们的应用程序中阅读了 GCD 的重要性。 请帮助我了解更多相关信息。
更新 这些图像来自我拍摄的相机。我想我需要调整它的大小。
【问题讨论】:
-
self.devices是您加载 tableView 的数组吗? -
@noobsmcgoobs 是的。那是持有 coredata 数据列表的人。
-
您的数据集有多大?具体来说 - 它是否如此之大以至于您实际上只需要在需要时才检索数据,还是可以将其保存在内存中?
-
还有一个问题,您使用的是自定义
UIImageView而不是默认contentView的imageView属性? -
对不起,我没有得到数据集的平均值。在哪里可以看到该数据集?
标签: ios objective-c uitableview core-data