Search code examples
iphoneiosoptimizationios6uicollectionview

bad performance while I load lots of images in UICollectionView


I have to display lots of images in UIcollectionView, may be greater than 200. Images are of big sizes. But there is serious performance issue out there. Scrolling became slow and sometimes it gets crashed . I dont know whats the best way to optimize in this kind of situation. I have custo cell which contains a UIImageView and I load image in method:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)CollectionView    cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCollectionCell *cell = [CollectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];

 if(cell==nil){

        NSLog(@"cell is nil");
    }



    NSLog(@"%@",NSStringFromCGRect(cell.frame));
    ImagesDb *imageObj = [folderImagesArr objectAtIndex:indexPath.row];

    ImagesDataDb *imageData = imageObj.data;



    cell.cellImage.image = imageData.orignalImage;




    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    [titleLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];



        }

    return cell;


}

Solution

  • The first thing that you can do it's limit the size of the image which use in cell, becouse now you load full size image. The best way if you display images which size equal or smaller cell.

    according to the documentation:

    In low-memory situations, image data may be purged from a UIImage object to free up memory on the system. This purging behavior affects only the image data stored internally by the UIImage object and not the object itself. When you attempt to draw an image whose data has been purged, the image object automatically reloads the data from its original file. This extra load step, however, may incur a small performance penalty.

    You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.