我正在UICollectionView一个显示相当多照片(50-200)的应用程序中使用,并且我遇到了让它变得活泼的问题(例如像照片应用程序那样活泼).
我有一个自定义UICollectionViewCell用UIImageView,因为它的子视图.我正在将文件系统中的图像加载UIImage.imageWithContentsOfFile:到单元格内的UIImageViews中.
我现在尝试了很多方法,但它们要么是有缺陷要么是性能问题.
注意:我正在使用RubyMotion,因此我将以Ruby风格编写任何代码片段.
首先,这是我的自定义UICollectionViewCell类供参考...
class CustomCell < UICollectionViewCell
def initWithFrame(rect)
super
@image_view = UIImageView.alloc.initWithFrame(self.bounds).tap do |iv|
iv.contentMode = UIViewContentModeScaleAspectFill
iv.clipsToBounds = true
iv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
self.contentView.addSubview(iv)
end
self
end
def prepareForReuse
super
@image_view.image = nil
end
def image=(image)
@image_view.image = image
end
end
Run Code Online (Sandbox Code Playgroud)
方法#1
保持简单..
def collectionView(collection_view, cellForItemAtIndexPath: index_path)
cell = collection_view.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: index_path)
image_path = @image_paths[index_path.row]
cell.image = UIImage.imageWithContentsOfFile(image_path)
end
Run Code Online (Sandbox Code Playgroud)
使用此功能可以向上/向下滚动.这是跳跃和缓慢.
方法#2
通过NSCache添加一些缓存...
def viewDidLoad
...
@images_cache …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的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很多细胞.我想加载所有单元格viewDidLoad,但UICollectionView只有当它们可见时才加载它的单元格.
有什么建议?
cells ×1
cocoa-touch ×1
ios ×1
ios6 ×1
objective-c ×1
performance ×1
rubymotion ×1
uiimage ×1
uiimageview ×1
uitableview ×1