简体   繁体   中英

The UICollectionView cells are jerking while scrolling the UICollectionView

Hi all i am a newbie in IOS Development and i have a UICollection where i am loading images with SDWebImageCache. The problem is my UICollectionview cell feels like jerking or shivering while scrolling fast. Below I post my code for loading images with SDWebImageCache

  [_homeCellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[dictionary objectForKey:@"user_image"],[QPCommonClass cropImageWithWidth:500 andHeight:500]]] placeholderImage:[UIImage imageNamed:@"defaultIcon"]];
    [_qpQuestionImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[dictionary objectForKey:@"image"],[QPCommonClass cropImageWithWidth:1400 andHeight:1200]]] placeholderImage:[UIImage imageNamed:@"question"] options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
        [_qpHomeActivityIndicator setHidden:YES];
        [_qpHomeActivityIndicator stopAnimating];

    }];

where _homeCellImage & _qpQuestionImage are UIImageViews

  1. Make sure to load the image in a background thread.
  2. Once the image is loaded make sure to cache it.

Now when you set the image via cellForItem, the image can be loaded directly from cache, and it should be a lot smoother.

Try to do like this--

[_homeCellImage setShowActivityIndicatorView:YES];

[_homeCellImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];

[_homeCellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[dictionary objectForKey:@"user_image"],[QPCommonClass cropImageWithWidth:500 andHeight:500]]] placeholderImage:[UIImage imageNamed:@"defaultIcon"] options:SDWebImageRefreshCached];

[_qpQuestionImage setShowActivityIndicatorView:YES];

[_qpQuestionImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];

[_qpQuestionImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[dictionary objectForKey:@"image"],[QPCommonClass cropImageWithWidth:1400 andHeight:1200]]] placeholderImage:[UIImage imageNamed:@"question"] options:SDWebImageRefreshCached];

You have to add these 2 lines after your dequeue.

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM