Simple utility method to create a image from video file. Specify the size of the image and the time interval.
UIImage *getImageFromMovieAtTime(AVURLAsset *videoAsset, CGSize imageSize,CMTime frameTime)
{
NSError *error =nil;
CMTime actualTime;
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:videoAsset];
imageGenerator.appliesPreferredTrackTransform = YES;
imageGenerator.maximumSize = imageSize;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:frameTime actualTime:&actualTime error:&error];
UIImage *movImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
if(error || movImage == nil)
{
DEBUGLOG(@"Error: %@",error);
}
DEBUGLOG(@"imagesize: %@",NSStringFromCGSize(movImage.size));
return movImage;
}
Usage:
UIImage *videoFrame = getImageFromMovieAtTime(videoAsset, CGSizeMake(width, height),kCMTimeZero);