Sunday, May 3, 2015

Work with vanilla UICollectionViewCell

Problem

Sometimes, you'll work with a very big UICollectionView, ex: A cell contains a UICollectionView, a cell contains a UITableView, a cell contains a bundle UIView and a lot of Auto Layout constraint.
Especially, in that cell contains UIImageView, when you scroll on the screen, it's laggy.

How to solve that

Determine issues

Why does it happen?
  • Rendering UIImage on UIImageView is take time.
  • Draw the Image when scrolling is take time.
  • Auto Layout is take time.
  • Draw UIImageView is take time.
  • Caching on disk is take time.
How I solve that?
  • For rendering UIImage on UIImageView, firstly, avoid use native setUIImage method or setImageWithURL of AFNetworking now. Why?. Look at SDWebImage and see what happen :)
  • For drawing the Image when scrolling, use below method when setup cell.
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
  • For Auto Layout, it is a problem on iOS 8, make the cell have auto layout scroll very laggy. So this is solution: Override 1 method in UICollectionViewCell class.
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    return layoutAttributes;
}
  • For the problem of UIImageView, some properties of it will decrease the performance, ex: clipToBounds
  • Writting and Reading on Disk is taked more time than Memory, therefore, take care when working with Caching.
Sourcec: http://kipalog.com/posts/ARyQ1xPVy2CG6coH711lFg

No comments:

Post a Comment