I am trying to create a video gallery.
To display videos I am using a UICollectionView
. Each UICollectionViewCell
has a background with video thumbnail. To generate a video thumbnail I am using a method with logic:
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0,15);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime timeRequested, CGImageRef image, CMTime timeActual, AVAssetImageGeneratorResult result, NSError *error)
{
NSLog(@"handler^()");
if (result == AVAssetImageGeneratorSucceeded)
{
thumbnail = [UIImage imageWithCGImage: image];
success(thumbnail);
}
else
{
failure(error);
}
};
CGSize maximumSize = CGSizeMake(CLIPBOARD_COLLECTION_VIEW_CELL_WIDTH, CLIPBOARD_COLLECTION_VIEW_CELL_HEIGHT);
generator.maximumSize = maximumSize;
NSLog(@"generateCGImagesAsynchronouslyForTimes:");
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler];
I have noticed that generateCGImagesAsynchronouslyForTimes
doesn't work fully asynchronously. There a time space between this method calls. This causes a big lag while I am loading table view cells. If I comment line [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler]
then there are no visible lag (nor images).
How can I solve this performance issue?