Skip to content

Instantly share code, notes, and snippets.

@phucnm
Last active December 10, 2015 03:36
Show Gist options
  • Save phucnm/9a91fda097eebeb6df52 to your computer and use it in GitHub Desktop.
Save phucnm/9a91fda097eebeb6df52 to your computer and use it in GitHub Desktop.
Get thumnail of a video from a URL at arbitrary time
// import AVFoundation
- (UIImage*)thumbnailFromVideo:(NSURL *)url atTime:(float)time {
AVAsset *asset = [AVAsset assetWithURL:url];
// get 3 decimal places
// ex: 3.39235350 --> 3.392
float nearest = roundf(time * 1000) / 1000;
// make CMTime from float
// because of 3 decimal places, we use 10000 (10^4)
// n decimal places, use 10^(n+1)
CMTime thumbnailTime = CMTimeMake(nearest, 10000);
// Get image from the video at the given time
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment