0

I have to load many images into UICollectionView. When i scroll the UICollectionView, I feel it got a little bit of lag when I scroll to next page. Is it because I loaded 12 images when it go next page? If yes, how can I solve it. Below is my code.

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

    static NSString *identifier = @"TestAZCollectionViewCell";

    TestAZCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    imgPath = [imgDir stringByAppendingPathComponent:[[itemArray objectAtIndex:indexPath.row]objectForKey:@"IM_ImgName"]];

    cell.cellImage.image = [UIImage imageWithContentsOfFile:imgPath];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_image.jpg"]];


    return cell;
}

anyone can help on this ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
vic3ai
  • 279
  • 1
  • 2
  • 10

3 Answers3

0

Try this method:

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

Or find some new method in :

UICollectionViewDelegate

UICollectionViewDataSource


I wish I could help you!

Steven Gardner
  • 234
  • 3
  • 9
0

It lags because the system needs to load an image from disk or network-> slow point here. To increase speed you can do following:

  • Use a different thread rather than the main thread to load an image object, when it done -> update UI via the main thread.

  • Image with big size cause slow UI scrolling as well as loading time -> resize the image to size of image view in your collection if possible.

  • Lazy loading, display cell with image holder first and update image when loading image completed

Trung Phan
  • 923
  • 10
  • 18
0

Finally i solve the problem. to make it scroll smoothly, i use "haneke" to display local image. hope can help other

vic3ai
  • 279
  • 1
  • 2
  • 10