我有一个自定义的UITableViewCell,它包含一个UIImageView和一个UILabel.单元格为320x104px,imageView占据整个区域,标签位于前面.只有8个细胞.
在ViewDidLoad中我预先创建所有需要的图像,并在字典中以正确的尺寸缓存它们.
当我滚动UITableView时,每次遇到新单元格时都会有明显的延迟.这对我来说毫无意义,因为它正在使用的图像已经被创建和缓存.所有我要求的单元格都是为了让UIImageView渲染图像.
我在xib中使用自定义单元格及其视图,并将我的UITableView配置为使用它:
[self.tableView registerNib:[UINib nibWithNibName:@"ActsCell"bundle:nil] forCellReuseIdentifier:myIdentifier];
细胞创建和配置:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* reuseIdentifier = @"ActsCell";
ActsCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(ActsCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Act* act = [self.acts objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.title.text = act.name;
cell.imageView.image = [self.imageCache objectForKey:act.uid];
}
Run Code Online (Sandbox Code Playgroud)
什么可能导致滞后?在完成所有时间密集型工作时,尝试执行任何异步操作似乎没有任何好处.
我的应用程序中有一个UICollectionView,每个单元格都是UIImageView和一些文本标签.问题是当我让UIImageViews显示他们的图像时,滚动性能很糟糕.它远不如UITableView的滚动体验,甚至没有UIImageView的相同UICollectionView.
我几个月前发现了这个问题,似乎找到了答案,但它是用RubyMotion编写的,我不明白.我试着看看如何将它转换为Xcode,但由于我从未使用过NSCache,所以有点难.海报上有还指出了这里关于实施,除了他们的解决方案的东西,但我不知道在哪里把这些代码要么.可能是因为我不理解第一个问题的代码.
有人能帮助将其翻译成Xcode吗?
def viewDidLoad
...
@images_cache = NSCache.alloc.init
@image_loading_queue = NSOperationQueue.alloc.init
@image_loading_queue.maxConcurrentOperationCount = 3
...
end
def collectionView(collection_view, cellForItemAtIndexPath: index_path)
cell = collection_view.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: index_path)
image_path = @image_paths[index_path.row]
if cached_image = @images_cache.objectForKey(image_path)
cell.image = cached_image
else
@operation = NSBlockOperation.blockOperationWithBlock lambda {
@image = UIImage.imageWithContentsOfFile(image_path)
Dispatch::Queue.main.async do
return unless collectionView.indexPathsForVisibleItems.containsObject(index_path)
@images_cache.setObject(@image, forKey: image_path)
cell = collectionView.cellForItemAtIndexPath(index_path)
cell.image = @image
end
}
@image_loading_queue.addOperation(@operation)
end
end
Run Code Online (Sandbox Code Playgroud)
UIImage *productImage = [[UIImage alloc] …Run Code Online (Sandbox Code Playgroud) performance objective-c rubymotion uicollectionview uicollectionviewcell