Generating thumbnails from AVPlayer ios

1.6k views Asked by At

I'm trying to generate a thumbnail image from a video in a specific timecode. I'm using AVPlayer and this is the method I wrote:

-(UIImageView *) generateCommentThumbnail: (CMTime) now{

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];
gen.appliesPreferredTrackTransform = YES;

NSError *error = nil;
CMTime actualTime;
[gen setRequestedTimeToleranceAfter:kCMTimeZero];
[gen setRequestedTimeToleranceBefore:kCMTimeZero];
CGImageRef image = [gen copyCGImageAtTime:now actualTime:&actualTime error:&error];


UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
UIImageView *iv = [[UIImageView alloc] initWithImage:thumb];

NSLog(@"%f , %f",CMTimeGetSeconds(now),CMTimeGetSeconds(actualTime));

NSLog(@"%@",error);
    return iv;

}

to get a frame accurate image, I used the setsetRequestedTimeToleranceBefore:kCMTimeZero and setRequestedTimeToleranceAfter:kCMTimeZero, but since then most of the times I get this error:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed"

Although, sometimes it works fine. When I print, when getting this error, "actualTime" I see it's "inf" or "nan" or just a very large number.

anyone ran in to this before?

Thanks, Yonathan

1

There are 1 answers

0
Murat Kaya On

Problem is you are creating so many instances so you need the deallocate