iosobjective-cuicollectionviewavassetimagegenerator

ios UICollectionView slow scrolling and memory warning


I am currently working on a project that involves a UICollectionView populated by AVAssets imported from a UIImagepickerController, after 10 or so item are in the collection, Scrolling becomes laggy and slow, and occasionally I receive memory warnings. I believe the problem to be in the thumbnail generation which happens in realtime, here is the code i use:

- (void) setAsset:(AVAsset *)asset
{
    _asset = asset;
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:_asset];
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    generate.appliesPreferredTrackTransform = YES;
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    self.VideoImageView.image = [UIImage imageWithCGImage:(imgRef)];
}

Is there another less "expensive way" to achieve this without delay? Any help on the matter would be greatly appreciated.


Solution

  • You can do the real time thumbnail generation on a background thread, and then jump back to the main thread when the operation is done to set the actual thumbnail.

    Right now you're doing everything on the main thread, which blocks the UI, and makes the scrolling jerky.