UICollectionview加载单元格时滚动波动

Avn*_*arr 33 objective-c ios xcode-instruments uicollectionview uicollectionviewcell

我在我的应用程序中使用了一个库UICollectionView.细胞大小约为70,70.我使用ALAssetsALAssetLibrary我在其中已经存储在列表中的画廊.

我使用通常的模式来填充单元格:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

  mycell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
  mycell.imageView.image = [[UIImage imageWithCGImage:[alassetList objectAtIndex:indexpath.row] thumbnail]];
  return mycell;
}
Run Code Online (Sandbox Code Playgroud)

我的画廊滚动波涛汹涌.我不明白为什么会这样.我已经尝试添加一个NSCache来缓存缩略图(想想可能创建图像很昂贵),但这对性能没有帮助.

我希望用户界面像股票应用程序一样黄油.

我现在怀疑它可能是某种东西UICollectionViewCell prepareForReuse可能会阻碍dequeueReusableCellWithReuseIdentifier方法,但使用我无法找到的工具.

还有其他可能造成这种情况的事吗?是否有一种"更快"的方式以更快的方式UICollectionViewCelldequeue它们做准备?

Avn*_*arr 96

因此,有滚动任何问题,应该做

出列后添加这两行

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
Run Code Online (Sandbox Code Playgroud)

  • 对不起,我该怎么办?您的链接指向包含大量内容的页面.如果我有++任何++滚动问题,我应该阅读所有内容吗?或者只是它有点滞后? (4认同)
  • @AvnerBarr感谢您的精彩回答,以及您对其他问题的回答http://stackoverflow.com/questions/16336772/我已经添加了一个小小的赏金来说谢谢大家:)欢呼 (4认同)

pre*_*lds 19

我认为"波动性"来自UIImage分配,而不是任何dequeueReusableCellWithReuseIdentifier方法.我尝试在后台线程上进行图像分配,看看是否会让事情变得更加白皙.

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  mycell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
     // Load image on a non-ui-blocking thread
    UIImage *image = [[UIImage imageWithCGImage:[alassetList objectAtIndex:indexpath.row] thumbnail]];

    dispatch_sync(dispatch_get_main_queue(), ^(void) {
        // Assign image back on the main thread
        mycell.imageView.image = image;
    });
  });

  return mycell;
}
Run Code Online (Sandbox Code Playgroud)

更多细节可以在这里找到:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html


Bea*_*ins 8

使用NSURLConnection加载图像 sendAsynchronousRequest:queue:completionHandler:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    [cell.imageView setImage:[UIImage imageWithData:data]];
}];
Run Code Online (Sandbox Code Playgroud)