0

I have a paging UICollectionView that scrolls through images. Each image fills the screen. For regular photos, my collectionView scrolls fluently but with panoramic shots, it begins to lag as I scroll through the images.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.tag = indexPath.row;

    PFObject *temp = [_dataArray objectAtIndex:indexPath.row];
    PFUser *user = [temp objectForKey:@"user"];
    PFFile *file = [temp objectForKey:@"image"];

    [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
        if (!error) {
            cell.selectedImageView.image = [UIImage imageWithData:data];
            self.navigationItem.title = [user objectForKey:@"Name"];
        }
    }];

    return cell;
}

As you can see I load the image in the background.

Is it possible I need to do something in willDisplayCell?. Thanks

0yeoj
  • 4,500
  • 3
  • 23
  • 41
Peter
  • 1,053
  • 13
  • 29

2 Answers2

0

You are loading data every time. try something to prevent that.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ...

    if (cell.selectedImageView.image == nil)
    {
        [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
            if (!error) {
                cell.selectedImageView.image = [UIImage imageWithData:data];
                self.navigationItem.title = [user objectForKey:@"Name"];
            }
        }];
    }

    return cell;
}

Also, sometimes it is just in simulator. Try resetting the simulator or Xcode then run again.

I've experience that sometimes, i even check for possible memory handling error but Restarting simulator did the work.

0yeoj
  • 4,500
  • 3
  • 23
  • 41
0

I never actually used Parse objects before but seems like it's because a cell tries to load multiple images at same time. I think you should move the logic that loads the image to cell and cancel the image loading when it is being reused in prepareForReuse. This idea is commonly used when you load a image in the cell. I will give you quick example and hopefully it gives you an idea.

in imageCell,

var file: PFFile? {
    didSet {
        if let f = file {
             [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
                if (!error) {
                     selectedImageView.image = [UIImage imageWithData:data];
                }
            }];
        }
    }
}

........

override func prepareForReuse() {
    super.prepareForReuse()
    if let f = file {
       f.cancel()
    }
}

in collectionView: cellForItemAtIndexPath:

.....
cell.file = file
.....
HMHero
  • 2,333
  • 19
  • 11