Xcode – UIcollectionView has slow jerky loading of images and scrolling

performanceuicollectionviewuiimagexcode

I have a UICollectionView with some images in it. It is very slow and jerky to load the images on the first scroll down. After that, when the images are loaded it is fine.

I guess the problem is that the images don't get loaded until the cell reaches the screen which causes a lag. I'm assuming what I need to do is load the images before they appear on the screen. I'm not sure how to do this and am obviously searching for the wrong thing as I can't find any answers. Please can someone point me in the right direction please??

Here is the code which loads the images…The images are in my app not downloaded or anything.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];

    return cell;
}

Thanks!

Best Solution

Look at this answer

Add these 2 lines after your dequeue

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